473,324 Members | 2,473 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,324 software developers and data experts.

Temporary and non-const member function

Dear Experts,

I have a question.

I wrote the following code

class A
{
public:
void g(){}
};

A f()
{
A a;
return a;
}

int main(int argc, char *argv[])
{
f().g();

return 0;
}
Here f() is temporary on which g() is called which is a non-const
member function of A.
So my doubt is, as we know temporary is always const ,so how it can
call a non-const member function?
Code above is getting compiled.

Thanks in advance,
Siddhu

May 4 '07 #1
7 1520
siddhu wrote:
I wrote the following code

class A
{
public:
void g(){}
};

A f()
{
A a;
return a;
}

int main(int argc, char *argv[])
{
f().g();

return 0;
}
Here f() is temporary on which g() is called which is a non-const
member function of A.
So my doubt is, as we know temporary is always const ,
Really? Who said that? Or, rather, where did you get that?

Anyway, you're incorrect. Temporaries are NOT const.
>so how it can
call a non-const member function?
Code above is getting compiled.
As it should.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 4 '07 #2
siddhu wrote:
Dear Experts,

I have a question.

I wrote the following code

class A
{
public:
void g(){}
};

A f()
{
A a;
return a;
}

int main(int argc, char *argv[])
{
f().g();

return 0;
}
Here f() is temporary on which g() is called which is a non-const
member function of A.
So my doubt is, as we know temporary is always const ,so how it can
call a non-const member function?
Code above is getting compiled.

As Victor said, temporaries are not const. However, i believe they can
only be bound to a non-const reference, hence your confusion.
May 4 '07 #3
red floyd wrote:
[..] i believe they
can only be bound to a non-const reference, hence your confusion.
Vice versa. Temporaries can only be bound to a reference to const
(a const reference).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 4 '07 #4
On May 4, 2:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
red floyd wrote:
[..] i believe they
can only be bound to a non-const reference, hence your confusion.

Vice versa. Temporaries can only be bound to a reference to const
(a const reference).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you all for clearing my confusion.

May 4 '07 #5
Victor Bazarov wrote:
red floyd wrote:
>[..] i believe they
can only be bound to a non-const reference, hence your confusion.

Vice versa. Temporaries can only be bound to a reference to const
(a const reference).
Typo. I meant to say const.
May 4 '07 #6
On May 4, 8:04 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Temporaries are NOT const.
Except when they are:-). Temporaries are rvalues, and rvalues
are funny beasts in C++. An rvalue of non class type doesn't
have const-ness (or volatile-ness); it's neither const nor
non-const. An rvalue of class type can be either const or
non-const; like everything else, it is non-const by default, but
if you qualify it as const, it becomes const.

We tend to think of them as non-const, because in many context,
there's no way to qualify them as const. In the actual example,
however, the reason the temporary is not const is because it
wasn't declared const. Had the function been declared:

A const f() ;

, the temporary would have been const.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 5 '07 #7
On May 4, 9:55 pm, siddhu <siddharth....@gmail.comwrote:
On May 4, 2:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
red floyd wrote:
[..] i believe they
can only be bound to a non-const reference, hence your confusion.
Vice versa. Temporaries can only be bound to a reference to const
(a const reference).
Thank you all for clearing my confusion.
Or adding to it, since the statements were incorrect. A
temporary of class type obeys exactly the same rules as a
non-temporary with respect to const-ness. You declared the
return type of the function non-const, so it is non-const. Had
you declared it const, it would have been const.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
May 5 '07 #8

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

Similar topics

2
by: Oswaldo Castro | last post by:
Hi All I found some files in mysql directory with names like #sql-358_2698.MYD. Is this a temporary file created by an alter table statement? May I delete it since all my tables are ok? ...
2
by: Ryan | last post by:
Just a quicky about temporarary tables. If using QA, when you create a temporary table, it gets dropped if you close the query. Otherwise you need to state 'DROP TABLE myTable' so that you can...
2
by: Yannick Turgeon | last post by:
Hello all, I'm using SS2K on W2K. Brieffing: Many months ago, I created a stored procedure only used by those with admin rights in SS. Now, someone else (without admin rights) has to run it....
8
by: Martijn van Oosterhout | last post by:
Currently you can create temporary tables that are deleted at the end of the session. But how about temporary views? It's just a table with a rule so I don't imagine it would be terribly difficult....
3
by: pinney.colton | last post by:
I would like to create a stored procedure which creates a temp table to store some XML. The # of fields of XML is dependent upon the contents of another table in the application, so the first part...
3
by: Chris | last post by:
All I am cross-posting, as I'm not sure if this is an issue for the data layer or the application layer. If this is unacceptable, can someone let me know so that I don't do this in future. ...
2
by: anon.asdf | last post by:
Hello! 1) =============================== When trying to define an array of std::string ... func( (std::string ) { std::string("ab"), std::string("cd"), std::string("ef") } , 3 ); ...
2
by: lee.walczak | last post by:
Hi, I am using Tkinter & the Kevin Walzer's TableList Wrapper for python implemented GUI: http://tkinter.unpythonic.net/wiki/TableListWrapper The TableList has been extremely useful in...
2
by: stefan.albert | last post by:
Hello Ravi, what you want to do is very tricky, but possible. I've had this same problem - we want to identify the top user of tempspace and eventually force him off when using too much space (FS...
6
by: Troels Arvin | last post by:
Hello, I have recently run a rather large data import where the imported data i pumped through some updatable views equipped with INSTEAD OF triggers. For various reasons, the exact same data...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.