How to use reference return by a method? | Bytes (2024)

Home Posts Topics Members FAQ

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::GetIns tance();
c2. MyClass obj = MyClass::GetIns tance();

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!

Nov 22 '06 #1

Subscribe Reply

9 How to use reference return by a method? | Bytes (1) 1240 How to use reference return by a method? | Bytes (2)

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.

Nov 22 '06 #2

Allen

I traced the addresses.

c1. MyClass& objRef = MyClass::GetIns tance();

&objRef is exactly equal to pInstance.

c2. MyClass obj = MyClass::GetIns tance();

&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?

Nov 22 '06 #3

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::tes tAdd()
{
MyClass obj = MyClass::GetIns tance();
CPPUNIT_ASSERT_ EQUAL( obj.Add(12, 13), 25 );
}

Nov 22 '06 #4

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::GetIns tance();
c2. MyClass obj = MyClass::GetIns tance();

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?

Nov 22 '06 #5

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: 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?

Nov 22 '06 #6

Allen

I understand it now. Thank you very much.

On 11ÔÂ22ÈÕ, ÏÂÎç5ʱ04·Ö, "Alf P. Steinbach" <a...@start.n o>
wrote:

* 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::GetIns tance();
c2. MyClass obj = MyClass::GetIns tance();
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,C 2 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?

Nov 22 '06 #7

Ondra Holub

"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::tes tAdd()
{
MyClass obj = MyClass::GetIns tance();
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::GetIns tance(); // Calls copy constructor
obj = MyClass::GetIns tance(); // Calls operator=

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

Nov 22 '06 #8

Allen

It is an interesting question.
I learn a lot from both of you.
Thank you.
"Ondra Holub дµÀ£º

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

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

Nov 22 '06 #9

terminator

Ondra Holub wrote:

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 &){};

Nov 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5 2938

Value/Reference Types - Oooooops!

by: Jerry Morton |last post by:

Hi, I'm new to C# and have run into a problem of my own making! I originally took from the documentation that as long as I didn't use the "ref" keyword in method declarations, that everything would be passed "by value". I now believe I was incorrect and that it's only types like int, bool, etc. that behave like that? i.e. things like...

.NET Framework

6 2834

Passing reference type with 'ref' vs. without

by: Lenn |last post by:

Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference types hold a reference to an object as opposed to object data itself. So, when reference type parameter is passed into a method, a copy of objects...

C# / C Sharp

13 2775

Passing reference type as method parameter

by: Maxim |last post by:

Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }

C# / C Sharp

1 3032

accessing reference parameter in managed C++ from C#

by: sklett |last post by:

STILL trying to wrap an unmanaged C++ class that is itself a wrapper to some COM stuff, not sure, it is littered with LPDISPATCH and InvokeHelper, etc. Problem is, when something goes wrong, I'm having a hard time debugging. In my managed C++ class(the one wrapping the unmanaged C++) I have a method that looks like this: bool...

.NET Framework

3 5754

About proxy classes generated when adding a web reference

by: MIGUEL |last post by:

Hi all, I'm quite lost with how adding web references to a project creates proxy classes. I've developed a web service with two classes inside and that contains three references to three DLLs. After building it up, I've deployed it on the web server. From my Windows application, I then add a web reference to that web service.

.NET Framework

5 10317

WSDL.exe vs. "Add Web Reference"

by: Mike Logan |last post by:

I used WSDL.exe to generate a client side web proxy for a web service, called the web service, got the results but an array returned by the web service is not in the results. However if I use "Add Web Reference" for the same service the same function works appropriately. Here is the client proxy generated from WSDL.exe ...

.NET Framework

6 5575

javascript passing by reference problem

by: ged |last post by:

Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an object collection and stanrd using it it starts to fall apart. Clearly there is something about javascript's usage of passing "By ref" that i am not...

Javascript

2 3046

using javascript closures to create singletons to ensure the survival of a reference to an HTML block

by: Jake Barnes |last post by:

Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is what I'm hoping to achieve. I've never before had to use Javascript closures, but now I do, so I'm making an effort to understand them. I've been...

Javascript

5 7808

Pass by reference to a WebService

by: David++ |last post by:

Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into a WebService is that essentially we are passing an address of an object which resides on the 'local machine' i.e. a local machine object address....

.NET Framework

8 2391

by: toton |last post by:

HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference (or better a const reference). for eg, const PointRange* points = cc.points(ptAligned);

C / C++

7747

What is ONU?

by: marktang |last post by:

ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...

General

7669

Changing the language in Windows 10

by: Hystou |last post by:

Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...

Windows Server

8179

Maximizing Business Potential: The Nexus of Website Design and Digital Marketing

by: jinu1996 |last post by:

In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...

Online Marketing

1 7740

The easy way to turn off automatic updates for Windows 10/11

by: Hystou |last post by:

Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...

Windows Server

8036

Discussion: How does Zigbee compare with other wireless protocols in smart home applications?

by: tracyyun |last post by:

Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...

General

1 5557

Access Europe - Using VBA to create a class based on a table - Wed 1 May

by: isladogs |last post by:

The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...

Microsoft Access / VBA

3702

Trying to create a lan-to-lan vpn between two differents networks

by: TSSRALBI |last post by:

Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

Networking - Hardware / Configuration

1 2167

transfer the data from one system to another through ip address

by: 6302768590 |last post by:

Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

C# / C Sharp

1 1269

How to add payments to a PHP MySQL app.

by: muto222 |last post by:

How can i add a mobile payment intergratation into php mysql website.

PHP

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisem*nts and analytics tracking please visit the page.

How to use reference return by a method? | Bytes (2024)

References

Top Articles
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 6401

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.