473,406 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

The type of *this

200 100+
Hello everyone,


I am confused what is the type of *this? Type of reference to instance or type of instance?

Looks like both code can compile, no warning messages. Any ideas?

Expand|Select|Wrap|Line Numbers
  1. class A{
  2.  
  3.     A& foo1()
  4.     {
  5.         return *this;
  6.     }
  7.  
  8.     A foo2()
  9.     {
  10.         return *this;
  11.     }
  12. };
  13.  

thanks in advance,
George
Dec 16 '07 #1
4 993
Hello everyone,


I am confused what is the type of *this? Type of reference to instance or type of instance?

Looks like both code can compile, no warning messages. Any ideas?

Expand|Select|Wrap|Line Numbers
  1. class A{
  2.  
  3.     A& foo1()
  4.     {
  5.         return *this;
  6.     }
  7.  
  8.     A foo2()
  9.     {
  10.         return *this;
  11.     }
  12. };
  13.  

thanks in advance,
George
As far as I think, when you are thrying the first code, i.e.,
Expand|Select|Wrap|Line Numbers
  1. A& foo1()
  2.     {
  3.         return *this;
  4.     }
  5.  
the original object is returned (with updatations if assigned), but for the second case, i.e.,
Expand|Select|Wrap|Line Numbers
  1. A foo1()
  2.     {
  3.         return *this;
  4.     }
  5.  
a copy of the original object is returned. The compiler will not complain because return by value is acceptable, but it will undoubtedly waste some memory space.
I haven't tried this at home, but it is true that is any other case if you try to return by address in the function body and do otherwise in function declaration, the compiler gives you an error!

Let's see what other people have to say on this....

Sagnik
Dec 16 '07 #2
George2
200 100+
Thanks Sagnik,


What does your below statement mean? Return address and do otherwise in declaration? Could you show me some pseudo code please?

but it is true that is any other case if you try to return by address in the function body and do otherwise in function declaration, the compiler gives you an error!

Sagnik

regards,
George
Dec 17 '07 #3
mac11
256 100+
I am confused what is the type of *this? Type of reference to instance or type of instance?
this is a pointer - the return type from your function prototype tells the compiler what you want to return

When you say you're going to return a reference you return a reference, when you say you're going to return a copy, you return a copy. The compiler decides based on what your code says its going to do.
Dec 17 '07 #4
George2
200 100+
Thanks mac11,


My question is answered.

this is a pointer - the return type from your function prototype tells the compiler what you want to return

When you say you're going to return a reference you return a reference, when you say you're going to return a copy, you return a copy. The compiler decides based on what your code says its going to do.

regards,
George
Dec 19 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dgaucher | last post by:
Hi, I want to consume a Web Service that returns a choice, but my C++ client always receives the same returned type. On the other hand, when I am using a Java client, it is working fine (of...
5
by: Axter | last post by:
I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem. One...
7
by: Együd Csaba | last post by:
Hi, I've a problem with some of my stored procs. My config is: RH7.1, Postgres 7.3.2 I had converted a few fields of a few tables from one type to another and after this I made all the...
2
by: Jason Cartwright | last post by:
I have an abstract base class and two derived classes that I want to serialize and deserialize with schema validation. When I serialize instances of the derived classes the XmlSerializer adds the...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
4
by: Harris | last post by:
Does anyone know how to get the underlying type of an Array type? Here's a bit of code demonstrating what I'm looking for... public Type getUnderlyingType( Type arrayType ) { if(...
3
by: david.kao | last post by:
I am trying to use Type.GetType(name of type in string format) to get an instance Type. For example: Type t = Type.GetType("System.Data.DataTable"). I have no trouble to get a value type For...
4
by: Ondrej Spanel | last post by:
The code below does not compile with .NET 2003, I get folowing error: w:\c\Pokusy\delegTemplArg\delegTemplArg.cpp(11) : error C2993: 'float' : illegal type for non-type template parameter 'x' ...
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
5
by: mknoll217 | last post by:
I am a newbie at XML. I have been given an xml document that I need to write the schema for, for a class assignment. The xml given to me is <?xml version="1.0"?> <Vendors> <Vendor> ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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...

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

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.