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
> 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