473,480 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reference to temporary

Hello all,

The code below is not legal (problem with the foo_t initializer list)
because:

"A reference that is not to 'const' cannot be bound to a non-lvalue"

How can I best achieve an effect similar to what this code attempts?

Thanks,
Dave

class bar_t
{
};

class foo_t
{
public:
foo_t(): ref(bar_t()) {}

private:
bar_t &ref;
};

void foo()
{
foo_t a;
}

int main()
{
foo();

return 0;
}

Jul 19 '05 #1
11 5586

"Dave" <be***********@yahoo.com> wrote in message news:vq************@news.supernews.com...
Hello all,

The code below is not legal (problem with the foo_t initializer list)
because:

"A reference that is not to 'const' cannot be bound to a non-lvalue"

How can I best achieve an effect similar to what this code attempts?


Use a const reference?
Jul 19 '05 #2
Dave wrote in news:vq************@news.supernews.com:
Hello all,

The code below is not legal (problem with the foo_t initializer list)
because:

"A reference that is not to 'const' cannot be bound to a non-lvalue"

How can I best achieve an effect similar to what this code attempts?

Thanks,
Dave

class bar_t
{
};

class foo_t
{
public:
foo_t(): ref(bar_t()) {}

private:
bar_t &ref;
};


You would have to tell us what is wrong with:

class foo_t
{
public:
foo_t() {}

private:
bar_t ref;
};

I.e. what problem you need to solve.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3
Dave wrote:
...
The code below is not legal (problem with the foo_t initializer list)
because:

"A reference that is not to 'const' cannot be bound to a non-lvalue"

How can I best achieve an effect similar to what this code attempts?
class bar_t
{
};

class foo_t
{
public:
foo_t(): ref(bar_t()) {}

private:
bar_t &ref;
};

void foo()
{
foo_t a;
}

int main()
{
foo();

return 0;
}
...


You can't do it this way. It you need a valid reference, which is a
member of some class, you need a valid lvalue object (i.e. a
non-temporary) to initialize it with. The object's lifetime should be at
least as long as the lifetime of the reference.

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #4
Ron Natalie wrote:
...
The code below is not legal (problem with the foo_t initializer list)
because:

"A reference that is not to 'const' cannot be bound to a non-lvalue"

How can I best achieve an effect similar to what this code attempts?


Use a const reference?
...


That will replace one problem with another. The code will compile, but
reference member will only remain valid until the constructor exits.
After that the reference is no longer valid. Something tells me that
that's not what OP intended to achieve.

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #5
"Dave" <be***********@yahoo.com> wrote in message
news:vq************@news.supernews.com...
Hello all,

The code below is not legal (problem with the foo_t initializer list)
because:

"A reference that is not to 'const' cannot be bound to a non-lvalue"

How can I best achieve an effect similar to what this code attempts?


[snip]

Just change the reference to an object:

class foo_t
{
private:
bar_t;
};
Jul 19 '05 #6
Hello all,

Well, I think I need to elaborate upon my previous post. My simplified
version of things did not provide enough context...

I am maintaining a very poor existing code base (isn't it the bane of all
our existences???). There is a class which contains as a data member an
ofstream object which is used for logging. This object is bound to a log
file upon object construction, so the stream's ready to write to by the time
we start doing any real work. So far so good... Where it gets messy is
that there is also a member function SetStream() to change the stream
dynamically. So, we may log to the initial stream for awhile, and then
decide to start logging to another stream. Seems like a reasonable thing to
want to do. However, SetStream() takes an ofstream parameter by value and
then assigns it to the member ofstream object. There are two problems
here - ofstream has neither a copy constructor nor an assignment operator,
so passing it as a parameter and assigning it are both illegal! This should
have never compiled, but we had a poor compiler (VC++ 6.0) and it was let
through. Now that we've upgraded to a more compliant compiler, the error is
finally getting caught.

So, I need to figure out a way to update this stream data member
dynamically. I have considered changing the data member from ofstream to
ofstream*, but there's a problem. The initial stream (upon object
construction) would have to be dynamically allocated, which means it would
have to be deallocated in the destructor. However, the client code that
uses this class passes non-dynamically allocated ofstream objects when it
calls SetStream(). So, we can start passing pointers rather than the
objects themselves, but the pointer can't be deallocated by the
destructor... I guess I could use a flag to mark whether the pointer points
to the orignal dynamically-allocated object or not, but there's got to be a
more elegant way...

My earlier attempt (in my original post) at trying to find a way to solve
this with reference members was off in la la land because an initializer
list can't initialize a reference member if the class in question has no
copy constructor. If this makes no sense, don't worry about it; there's no
need to go back and look at the original post. It was headed in the wrong
direction...

Thanks again,
Dave
Jul 19 '05 #7
"Dave" <be***********@yahoo.com> wrote in message
news:vq************@news.supernews.com...
Hello all,

Well, I think I need to elaborate upon my previous post. My simplified
version of things did not provide enough context...

I am maintaining a very poor existing code base (isn't it the bane of all
our existences???). There is a class which contains as a data member an
ofstream object which is used for logging. This object is bound to a log
file upon object construction, so the stream's ready to write to by the time we start doing any real work. So far so good... Where it gets messy is
that there is also a member function SetStream() to change the stream
dynamically. So, we may log to the initial stream for awhile, and then
decide to start logging to another stream. Seems like a reasonable thing to want to do. However, SetStream() takes an ofstream parameter by value and
then assigns it to the member ofstream object. There are two problems
here - ofstream has neither a copy constructor nor an assignment operator,
so passing it as a parameter and assigning it are both illegal! This should have never compiled, but we had a poor compiler (VC++ 6.0) and it was let
through. Now that we've upgraded to a more compliant compiler, the error is finally getting caught.

So, I need to figure out a way to update this stream data member
dynamically. I have considered changing the data member from ofstream to
ofstream*, but there's a problem. The initial stream (upon object
construction) would have to be dynamically allocated,
Why?

std::ostream *m_pstr;
void SetStream(std::ostream &new_stream) {m_pstr = &new_stream;} // pass by
reference and store the address

Of course the life of the stream has to be longer than the life of the
object, but that can't be helped. To use it:

*m_pstr << "whatever";

which means it would have to be deallocated in the destructor. However, the client code that
uses this class passes non-dynamically allocated ofstream objects when it
calls SetStream(). So, we can start passing pointers rather than the
objects themselves, but the pointer can't be deallocated by the
destructor... I guess I could use a flag to mark whether the pointer points to the orignal dynamically-allocated object or not, but there's got to be a more elegant way...

My earlier attempt (in my original post) at trying to find a way to solve
this with reference members was off in la la land because an initializer
list can't initialize a reference member if the class in question has no
copy constructor. If this makes no sense, don't worry about it; there's no need to go back and look at the original post. It was headed in the wrong
direction...

Thanks again,
Dave


--
Cy
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #8

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:ey*******************@twister.nyroc.rr.com...
"Dave" <be***********@yahoo.com> wrote in message
news:vq************@news.supernews.com...
Hello all,

Well, I think I need to elaborate upon my previous post. My simplified
version of things did not provide enough context...

I am maintaining a very poor existing code base (isn't it the bane of all our existences???). There is a class which contains as a data member an
ofstream object which is used for logging. This object is bound to a log file upon object construction, so the stream's ready to write to by the time
we start doing any real work. So far so good... Where it gets messy is
that there is also a member function SetStream() to change the stream
dynamically. So, we may log to the initial stream for awhile, and then
decide to start logging to another stream. Seems like a reasonable thing to
want to do. However, SetStream() takes an ofstream parameter by value
and then assigns it to the member ofstream object. There are two problems
here - ofstream has neither a copy constructor nor an assignment operator, so passing it as a parameter and assigning it are both illegal! This

should
have never compiled, but we had a poor compiler (VC++ 6.0) and it was let through. Now that we've upgraded to a more compliant compiler, the error is
finally getting caught.

So, I need to figure out a way to update this stream data member
dynamically. I have considered changing the data member from ofstream
to ofstream*, but there's a problem. The initial stream (upon object
construction) would have to be dynamically allocated,


Why?


What other option do I have for getting an initial per-object stream to set
the member pointer to point at?

std::ostream *m_pstr;
void SetStream(std::ostream &new_stream) {m_pstr = &new_stream;} // pass by reference and store the address

Of course the life of the stream has to be longer than the life of the
object, but that can't be helped. To use it:

*m_pstr << "whatever";

which means it would
have to be deallocated in the destructor. However, the client code that
uses this class passes non-dynamically allocated ofstream objects when it calls SetStream(). So, we can start passing pointers rather than the
objects themselves, but the pointer can't be deallocated by the
destructor... I guess I could use a flag to mark whether the pointer points
to the orignal dynamically-allocated object or not, but there's got to be a
more elegant way...

My earlier attempt (in my original post) at trying to find a way to

solve this with reference members was off in la la land because an initializer
list can't initialize a reference member if the class in question has no
copy constructor. If this makes no sense, don't worry about it; there's

no
need to go back and look at the original post. It was headed in the wrong direction...

Thanks again,
Dave


--
Cy
http://home.rochester.rr.com/cyhome/

Jul 19 '05 #9
"Dave" <be***********@yahoo.com> wrote in message
news:vq************@news.supernews.com...

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:ey*******************@twister.nyroc.rr.com...
"Dave" <be***********@yahoo.com> wrote in message
news:vq************@news.supernews.com...
Hello all,

Well, I think I need to elaborate upon my previous post. My simplified version of things did not provide enough context...

I am maintaining a very poor existing code base (isn't it the bane of all our existences???). There is a class which contains as a data member an ofstream object which is used for logging. This object is bound to a log file upon object construction, so the stream's ready to write to by the
time
we start doing any real work. So far so good... Where it gets messy
is that there is also a member function SetStream() to change the stream
dynamically. So, we may log to the initial stream for awhile, and then decide to start logging to another stream. Seems like a reasonable
thing
to
want to do. However, SetStream() takes an ofstream parameter by value

and then assigns it to the member ofstream object. There are two problems
here - ofstream has neither a copy constructor nor an assignment operator, so passing it as a parameter and assigning it are both illegal! This

should
have never compiled, but we had a poor compiler (VC++ 6.0) and it was let through. Now that we've upgraded to a more compliant compiler, the error
is
finally getting caught.

So, I need to figure out a way to update this stream data member
dynamically. I have considered changing the data member from ofstream

to ofstream*, but there's a problem. The initial stream (upon object
construction) would have to be dynamically allocated,


Why?


What other option do I have for getting an initial per-object stream to

set the member pointer to point at?

I showed you but you snipped that part out! LOL

Pass a reference in the argument list and then take its address. I've done
this before and it works fine.

Maybe my previous thing was a little cryptic. Here is a more explicit
example:

class Funky
{
private:
std::ostream *m_pstr;

public:
Funky(std::ostream &ostr) : m_pstr(&ostr) {} // note & operator

void SetStream(std::ostream &ostr) {m_pstr = &ostr;} // & operator again
};

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #10
Dave wrote:
What other option do I have for getting an initial per-object stream to set
the member pointer to point at?


You could have a static object. e.g.

class foo_t
{
static bar_t default_bar;
bar_t *my_bar;
public:
foo_t() : my_bar(&default_bar) { }
void set_bar(my_bar*b) { my_bar = b; }
bar_t *get_bar() const { return my_bar; }
};

....
bar_t foo_t::my_bar;

Doesn't even need to be static for that matter.

Jul 19 '05 #11
Calum wrote:
Dave wrote:
What other option do I have for getting an initial per-object stream
to set
the member pointer to point at?
bar_t foo_t::my_bar;


I meant

bar_t foo_t::default_bar;

I clearly have not tested this code but you get the idea?

Jul 19 '05 #12

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

Similar topics

3
1934
by: ded' | last post by:
Hello ! I've read in a magazine "reference parameter in operator= must be const, because in C++, temporary objects are const" and then my operator would not work with temporary objets. But,...
5
3722
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
6
1698
by: JKop | last post by:
unsigned int CheesePlain(void) { unsigned int const chalk = 42; return chalk; } unsigned int& CheeseRef(void) {
14
1901
by: Arik Funke | last post by:
Hi together, following code does compile fine on MS VC but not on gcc. What am I doing wrong? Is there a universal coding standard? Cheers, Arik --- class abc {
10
13708
by: sam | last post by:
Hi, I m wondering why I can't declare reference variable for default value in a function argument variable? eg. class A { void f(string &str="");
8
3088
by: Tim Clacy | last post by:
1) Is this initialising the reference 'u' to the address of the literal '2' or to the address 0x00000002? unsigned const& u = 2; 2) What is the different between the initialisation of 'u'...
10
3408
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
7
5756
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
5
449
by: George2 | last post by:
Hello everyone, This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review whether it is correct and feel free to...
3
5790
by: George2 | last post by:
Hello everyone, 1. Returning non-const reference to function local object is not correct. But is it correct to return const reference to function local object? 2. If in (1), it is correct...
0
7049
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
6912
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
7052
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
7092
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
6981
tracyyun
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...
0
5348
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,...
1
4790
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...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.