Prohibit pass by value

B

Boni

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::blush:stream& operator<< (std::blush:stream& 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);


};
 
K

Kevin Frey

Firstly, I'd be interested in knowing *why* you want to prevent this, since
that seems more like the problem to me.

Kevin
 
B

Boni

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
 
T

Tom Widmer [VC++ MVP]

Boni said:
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

{

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

{

cout<<"bhcadshhvfah"<<endl;

};

There shouldn't be a ; after a function definition.
std::blush:stream& operator<< (std::blush:stream& 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);

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
 
T

Tom Widmer [VC++ MVP]

Boni said:
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.

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

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top