| View previous topic :: View next topic |
| Author |
Message |
Wolfgang Meyer Guest
|
Posted: Fri Apr 23, 2004 12:38 am Post subject: Enforce constness of object copied with copy construcor? |
|
|
Is there a way to enforce that for a class A
class A
{
public:
A();
A(const A& init); // (1)
A(A& init; // (2)
...
};
a value that is constructed using copy constructor (1) may only be
used like a const object?
Example:
const A a;
const A b( a ); // okay
A b(a); // I want this to cause a compile time error
A a2;
A b2( a2 ); // okay
I know, this somewhat defeats the purpose of a copy constructor.
But such an enforcement would be very handy for me. Maybe with some
template meta-programming this could be archieved?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Migel Saavedra Guest
|
Posted: Sat Apr 24, 2004 12:01 am Post subject: Re: Enforce constness of object copied with copy construcor? |
|
|
"Wolfgang Meyer" <wolfgang (AT) meijer (DOT) de> wrote
| Quote: | Is there a way to enforce that for a class A
class A
{
public:
A();
A(const A& init); // (1)
A(A& init; // (2)
...
};
a value that is constructed using copy constructor (1) may only be
used like a const object?
Example:
const A a;
const A b( a ); // okay
A b(a); // I want this to cause a compile time error
A a2;
A b2( a2 ); // okay
|
One possible solution could be to forbid public copying altogether
and leave cloning to the class itself:
class A
{
private:
A( A const& Other );
void operator=( A const& Other );
public:
A();
A const* Clone() const
{
return new A(*this);
}
};
AFAIK, this will work as you want.
The downside of this is that you will use dynamic memory, so someone
will have to care to delete the clone.
| Quote: | But such an enforcement would be very handy for me. Maybe with some
template meta-programming this could be archieved?
|
"Some template meta-programming" seems to become a politically
correct euphemism for "hack" :-)
Ivan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michiel Salters Guest
|
Posted: Sat Apr 24, 2004 12:20 am Post subject: Re: Enforce constness of object copied with copy construcor? |
|
|
[email]wolfgang (AT) meijer (DOT) de[/email] (Wolfgang Meyer) wrote in message news:<6311ef2e.0404220353.2a1107b0 (AT) posting (DOT) google.com>...
| Quote: | Is there a way to enforce that for a class A
class A
{
public:
A();
A(const A& init); // (1)
A(A& init; // (2)
...
};
a value that is constructed using copy constructor (1) may only be
used like a const object?
|
No, not at compile time. Of course, at run-time every non-const method
of A could check a m_copyctor_used flag.
| Quote: | Example:
const A a;
const A b( a ); // okay
A b(a); // I want this to cause a compile time error
A a2;
A b2( a2 ); // okay
I know, this somewhat defeats the purpose of a copy constructor.
But such an enforcement would be very handy for me. Maybe with some
template meta-programming this could be archieved?
|
Not even needed:
class A;
class const_A { // invariant part
public:
const_A();
const_A( A const& rhs ) // slicing the modifiable part
foo() const;
};
class A : public const_A {
public:
A();
bar(); // !const
private:
A( const& A ); // supress the copy ctor
A( const_A const& rhs ); // backdoor also unavailable
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Wolfgang Meyer Guest
|
Posted: Tue Apr 27, 2004 10:35 am Post subject: Re: Enforce constness of object copied with copy construcor? |
|
|
[email]Michiel.Salters (AT) logicacmg (DOT) com[/email] (Michiel Salters) wrote in message news:<fcaee77e.0404230708.595e3ee8 (AT) posting (DOT) google.com>...
| Quote: | Not even needed:
class A;
class const_A { // invariant part
public:
const_A();
const_A( A const& rhs ) // slicing the modifiable part
foo() const;
};
class A : public const_A {
public:
A();
bar(); // !const
private:
A( const& A ); // supress the copy ctor
A( const_A const& rhs ); // backdoor also unavailable
};
|
Great! On second reading I realized that this is exactly what I
needed. Other than a solution with a clone-method, this will allow me
to store const_A's in STL containers. - Thanks a lot!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|