| View previous topic :: View next topic |
| Author |
Message |
Allen Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: How to use reference return by a method? |
|
|
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! |
|
| Back to top |
|
 |
Ondra Holub Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: How to use reference return by a method? |
|
|
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. |
|
| Back to top |
|
 |
Allen Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: How to use reference return by a method? |
|
|
I traced the addresses.
| Quote: | c1. MyClass& objRef = MyClass::GetInstance();
&objRef is exactly equal to pInstance. |
| Quote: | 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? |
|
| Back to top |
|
 |
Allen Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: How to use reference return by a method? |
|
|
"Ondra Holub 写道:
"
| Quote: | 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 ©) {};
void operator=(const MyClass ©) {};
But the following lines are still compiled ok.
void MyTestCase::testAdd()
{
MyClass obj = MyClass::GetInstance();
CPPUNIT_ASSERT_EQUAL( obj.Add(12, 13), 25 );
} |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: How to use reference return by a method? |
|
|
* Allen:
| Quote: | 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: 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? |
|
| Back to top |
|
 |
Allen Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: How to use reference return by a method? |
|
|
I understand it now. Thank you very much.
On 11月22日, 下午5时04分, "Alf P. Steinbach" <a...@start.no>
wrote:
| Quote: | * 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: 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? |
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: How to use reference return by a method? |
|
|
* Ondra Holub:
| Quote: | 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: 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? |
|
| Back to top |
|
 |
|