Copy constructor question

B

Bart Simpson

I have a class that has a member that is a const reference:

class MyClass
{
public:
MyClass(const AnotherClass& ac);
MyClass(const MyClass& mc);
//....

private:
const AnotherClass &m_reference ;
};


How do I implement the copy constructor? (I get compilation erros with
this):

MyClass::MyClass(const MyClass& mc)
:m_reference(mc.m_reference)
{
//..
Copy();
}

Compiler error: Error error C3646: 'm_reference' : unknown override
specifier
 
Z

Zeppe

Bart said:
I have a class that has a member that is a const reference:

class MyClass
{
public:
MyClass(const AnotherClass& ac);
MyClass(const MyClass& mc);
//....

private:
const AnotherClass &m_reference ;
};


How do I implement the copy constructor? (I get compilation erros with
this):

you can't.

Use a pointer instead of a reference if you want to copy the object
properly (i.e., modifying the reference member).

Regards,

Zeppe
 
V

Victor Bazarov

Zeppe said:
you can't.

Use a pointer instead of a reference if you want to copy the object
properly (i.e., modifying the reference member).

Huh? Compile this (and then explain):

class Foo {};
class Bar {
Foo const & foo;
public:
Bar(Bar const& b) : foo(b.foo) {}
Bar(Foo const& f) : foo(f) {}
};

int main() {
Foo foo;
Bar bar(foo);
Bar barbar(bar);
}

V
 
Z

Zeppe

Victor said:
Huh? Compile this (and then explain):

Sorry, sorry! Gosh! I was reading the post, and then I just saw the
reference member and I suddenly thought about the operator=... stupid
me, I have no excuse, I should really pay attention before posting. Even
because my sentence was totally unrelated to the error...

To the OP: the code seems fine to me, in which line are you experiencing
the error? It seems the effect of some Microsoft extension (CLR)...

Regards,

Zeppe
 
V

Victor Bazarov

Zeppe said:
[..]
To the OP: the code seems fine to me, in which line are you
experiencing the error? It seems the effect of some Microsoft
extension (CLR)...

Did you miss the OP's explanation? It was a missing colon in

Blah(const Boo& boo) m_boo(boo) {}

instead of needed

Blah(const Boo& boo) : m_boo(boo) {}

:)

V
 
Z

Zeppe

Victor said:
Zeppe said:
[..]
To the OP: the code seems fine to me, in which line are you
experiencing the error? It seems the effect of some Microsoft
extension (CLR)...

Did you miss the OP's explanation? It was a missing colon in

Blah(const Boo& boo) m_boo(boo) {}

instead of needed

Blah(const Boo& boo) : m_boo(boo) {}

:)

V

Yeah, the funny thing is, the code he posted in the ng was correct.
Quite hard for anybody to find the error ;)

Regards,

Zeppe
 
J

James Kanze

I have a class that has a member that is a const reference:
class MyClass
{
public:
MyClass(const AnotherClass& ac);
MyClass(const MyClass& mc);
//....

private:
const AnotherClass &m_reference ;
};
How do I implement the copy constructor? (I get compilation erros with
this):
MyClass::MyClass(const MyClass& mc)
:m_reference(mc.m_reference)
{
//..
Copy();
}
Compiler error: Error error C3646: 'm_reference' : unknown override
specifier

With what compiler? It looks fine to me, and compiles without
error or warning with g++ (4.1.0), Sun CC (5.8) and VC++ (8).
 
J

James Kanze

Zeppe said:
[..]
To the OP: the code seems fine to me, in which line are you
experiencing the error? It seems the effect of some Microsoft
extension (CLR)...
Did you miss the OP's explanation? It was a missing colon in
Blah(const Boo& boo) m_boo(boo) {}
instead of needed

Blah(const Boo& boo) : m_boo(boo) {}

No it wasn't. I copy/pasted the code directly from his message,
and compiled it successfully with three different compilers. So
either he's using some strange options (or a compiler I don't
have access to), as Zeppe suggests, or the code he posted isn't
the code causing the problem.
 
V

Victor Bazarov

James said:
Zeppe said:
[..]
To the OP: the code seems fine to me, in which line are you
experiencing the error? It seems the effect of some Microsoft
extension (CLR)...
Did you miss the OP's explanation? It was a missing colon in
Blah(const Boo& boo) m_boo(boo) {}
instead of needed

Blah(const Boo& boo) : m_boo(boo) {}

No it wasn't.

Yes, it was. Go read the OP's explanation before arguing again.
I copy/pasted the code directly from his message,

I wasn't talking of the code posted. I was talking about the
explanation the OP gave about the error. Jeez, are you just here
to argue for the sake of disagreeing?
and compiled it successfully with three different compilers. So
either he's using some strange options (or a compiler I don't
have access to), as Zeppe suggests, or the code he posted isn't
the code causing the problem.

V
 

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

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top