Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2006

Tip: Looking for answers? Try searching our database.

Prohibit pass by value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Boni - 01 Mar 2006 00:26 GMT
Dear all,
can I prohibit that my class will be passed by value (i.e created a copy on
stack when function is called)? I want to prohibit passing my class by
value. I thougth that declare a default constructor to private would do the
job, but no.
When myfunc is called some construcor goes called, but not the explicit
one.I have noidea which one is it. I am running out of ideas.
With best wishes,
Boni
Any ideas?
template< typename T> void myfunc(const templclass<T> a){

..

}

template< typename T> struct templclass

{

explicit templclass(std::string Name_, T DefaultVal_=0)

{

cout<<"bhcadshhvfah"<<endl;

};

std::ostream& operator<< (std::ostream& os)const {

return os<<m_Value;

};

//assignment operator

templclass& operator = ( const T a ){

m_Value=a;

return *this;

}

operator T ( ){

return m_Value;

}

private:

templclass();

T m_Value;

templclass( T);

};
Kevin Frey - 01 Mar 2006 03:09 GMT
Firstly, I'd be interested in knowing *why* you want to prevent this, since
that seems more like the problem to me.

Kevin

> Dear all,
> can I prohibit that my class will be passed by value (i.e created a copy
[quoted text clipped - 55 lines]
>
> };
Boni - 01 Mar 2006 10:16 GMT
Hi Kevin,
I found the solution, the constructor signature was wrong. should be
templclass( const T&); and also assignment op should be declared as private.
Now to your question. Sometimes you want to prohibit user to create objects
implicitely. Example: such object will be incorrectly initialized (as I
wrote compiler generates constructor without arguments) . There are many
such situations.
regards,
Boni
> Firstly, I'd be interested in knowing *why* you want to prevent this,
> since that seems more like the problem to me.
[quoted text clipped - 60 lines]
>>
>> };
Tom Widmer [VC++ MVP] - 01 Mar 2006 13:39 GMT
> Hi Kevin,
> I found the solution, the constructor signature was wrong. should be
[quoted text clipped - 3 lines]
> wrote compiler generates constructor without arguments) . There are many
> such situations.

To give you the best solution:
http://www.boost.org/libs/utility/utility.htm#Class_noncopyable

Tom
Tom Widmer [VC++ MVP] - 01 Mar 2006 11:05 GMT
> Dear all,
> can I prohibit that my class will be passed by value (i.e created a copy on
> stack when function is called)? I want to prohibit passing my class by
> value. I thougth that declare a default constructor to private would do the
> job, but no.

You need to make the copy constructor private.

> When myfunc is called some construcor goes called, but not the explicit
> one.I have noidea which one is it. I am running out of ideas.

The copy constructor is auto-generated by the compiler, and that's the
one that is being called.

> template< typename T> struct templclass
>
[quoted text clipped - 7 lines]
>
> };

There shouldn't be a ; after a function definition.

> std::ostream& operator<< (std::ostream& os)const {
>
[quoted text clipped - 25 lines]
>
> templclass( T);

Why do you have that private constructor?

Add the private copy constructor here:
templclass(templclass const&); // no definition

You should probably also add the default copy-assignment operator:
templclass& operator=(templclass const&); //no definition

Tom

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.