How to use reference return by a method?

A

Allen

class MyClass
{
private:
MyClass(void);

public:
static MyClass* pInstance;
static MyClass& GetInstance();
virtual ~MyClass();

public:
int Add(int op1, int op2);

};

I can use these ways:
c1. MyClass& objRef = MyClass::GetInstance();
c2. MyClass obj = MyClass::GetInstance();

The c1 is easy to understand, it uses a reference objRef assignment.
While the c2 is difficult to understand to me.
The constructor of MyClass is private. And C2 uses value assignment.
Who can explaint it?
Thank you!
 
O

Ondra Holub

c2 makes a copy. Or more precisely it calls operator= (if such operator
is available). I think your class should prohibit usage of operator=
(declare it as private without implementation) and you should use only
c1 version.
 
A

Allen

I traced the addresses.
c1. MyClass& objRef = MyClass::GetInstance();
&objRef is exactly equal to pInstance.
c2. MyClass obj = MyClass::GetInstance();
&obj is not equal to pInstance.

Therefore, in the case of c2, obj is a new object. But the constructor
of MyClass is private.
Why does assignment is allowed? Can I make a conclusion that assigment
does not need
call constructor?
 
A

Allen

"Ondra Holub дµÀ£º
"
c2 makes a copy. Or more precisely it calls operator= (if such operator
is available). I think your class should prohibit usage of operator=
(declare it as private without implementation) and you should use only
c1 version.

Thank you.

Yes, I want to prevent the use of copy method. So I declear these two
methods:

private:
MyClass(const MyClass &copy) {};
void operator=(const MyClass &copy) {};

But the following lines are still compiled ok.
void MyTestCase::testAdd()
{
MyClass obj = MyClass::GetInstance();
CPPUNIT_ASSERT_EQUAL( obj.Add(12, 13), 25 );
}
 
A

Alf P. Steinbach

* Allen:
class MyClass
{
private:
MyClass(void);

public:
static MyClass* pInstance;
static MyClass& GetInstance();
virtual ~MyClass();

public:
int Add(int op1, int op2);

};

I can use these ways:
c1. MyClass& objRef = MyClass::GetInstance();
c2. MyClass obj = MyClass::GetInstance();

The c1 is easy to understand, it uses a reference objRef assignment.
While the c2 is difficult to understand to me.
The constructor of MyClass is private. And C2 uses value assignment.

No, C2 uses copy construction.

You have an automatically generated copy constructor, since you haven't
provided or declared one.

You can do

class MyClass
{
private:
MyClass() {}
MyClass( MyClass const& ); // No copy constructor.
MyClass& operator=( MyClass const& ); // No assignment.
public:
static MyClass& theInstance()
{
static MyClass instance;
return instance;
}

int add( int op1, int op2 ) { ... }
};

although a class providing arithmetic operations is seemingly not
consistent with being a singleton.
 
A

Alf P. Steinbach

* Ondra Holub:
c2 makes a copy. Or more precisely it calls operator= (if such operator
is available).

No it doesn't.

Also, please quote some context when you reply (this is Usenet, not a
chat group).
 
A

Allen

I understand it now. Thank you very much.

* Allen:







You have an automatically generated copy constructor, since you haven't
provided or declared one.

You can do

class MyClass
{
private:
MyClass() {}
MyClass( MyClass const& ); // No copy constructor.
MyClass& operator=( MyClass const& ); // No assignment.
public:
static MyClass& theInstance()
{
static MyClass instance;
return instance;
}

int add( int op1, int op2 ) { ... }
};

although a class providing arithmetic operations is seemingly not
consistent with being a singleton.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
O

Ondra Holub

"Ondra Holub дµÀ£º
"

Thank you.

Yes, I want to prevent the use of copy method. So I declear these two
methods:

private:
MyClass(const MyClass &copy) {};
void operator=(const MyClass &copy) {};

But the following lines are still compiled ok.
void MyTestCase::testAdd()
{
MyClass obj = MyClass::GetInstance();
CPPUNIT_ASSERT_EQUAL( obj.Add(12, 13), 25 );
}

I was wrong (as Alf P. Steinbach mentioned in other post). It works
this way:

MyClass obj = MyClass::GetInstance(); // Calls copy constructor
obj = MyClass::GetInstance(); // Calls operator=

So copy constructor should be also private. Sorry for confusing you.
 
A

Allen

It is an interesting question.
I learn a lot from both of you.
Thank you.


"Ondra Holub дµÀ£º
 
T

terminator

Ondra said:
c2 makes a copy. Or more precisely it calls operator= (if such operator
is available). I think your class should prohibit usage of operator=
(declare it as private without implementation) and you should use only
c1 version.

c2 does not assign(call operator=) it constructs a MyClass via built-in
copy-constructor
you shoud add this to your MyClass if you want c2 not to compile:

private:
MyClass(MyClass &){};
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top