Prohibit pass by value

  • Thread starter Thread starter Boni
  • Start date Start date
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::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);


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

Kevin
 
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
 
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::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);

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