473,387 Members | 1,834 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,387 software developers and data experts.

Invoking methods on objects returned from functions

Hi!

I've been trying to figure this out, but can't seem to find an answer.
Most likely it's some weird bug in my code, but since I don't have
access to the Standard, I wanted to cover my bases.. basically the
situation is that I'm writing a logging facility, class Log. Instances
of the class can be invoked to read a message by using the method
Read() that enables the overloaded op<<. The trick is, Read() does not
necessarily return the Log reference (*this), but a reference to
another object, a Logstream. So the code looks something like

logger.Read() << "This is to be " << "logged.";
return 0;

Now, the problem is: if I return *this in Read(), everything works
fine. However, if I return a Logstream&, the program skips directly to
the return instruction. Even if I disable the op<< for Logstream,
there's no complaint, which suggests that op<< isn't applied to the
result of logger() -but when returning a Log object it is, with all
other code the exact same.

So, the question is: does anyone know of anything in the Standard that
causes this, or is there just a bug in the program -and if so, any
ideas where to look? I'll post followups if I figure it out.
Jul 19 '05 #1
6 1623
> I've been trying to figure this out, but can't seem to find an answer.
Most likely it's some weird bug in my code
Most likely, yes :)
, but since I don't have
access to the Standard, I wanted to cover my bases.. basically the
situation is that I'm writing a logging facility, class Log.
Please, try to post some code next time.
Instances
of the class can be invoked to read a message by using the method
Read() that enables the overloaded op<<.
A method (btw, there is no such thing in C++, it is called "member
function". Whatever) cannot "enable the overloaded op<<". A function
overloads another.
The trick is, Read() does not
necessarily return the Log reference (*this), but a reference to
another object, a Logstream. So the code looks something like
What is Logstream ? Is there an overloaded operator<< for it? If not, does
it inherit from Log? If Logstream is an independent class with no
overloaded operator<< and if no operator<<() can be applied to it, you
should have gotten a compilation error.
logger.Read() << "This is to be " << "logged.";
return 0; Now, the problem is: if I return *this in Read(), everything works
fine. However, if I return a Logstream&, the program skips directly to
the return instruction.
That is impossible. The program cannot "skip" a statement because of a
return value.
Even if I disable the op<< for Logstream,
there's no complaint, which suggests that op<< isn't applied to the
result of logger() -but when returning a Log object it is, with all
other code the exact same.
That is weird. It means the compiler found another operator<<() somewhere
which is appropriatly overloaded and has a higher precedence than yours.
So, the question is: does anyone know of anything in the Standard that
causes this,
Nope.
or is there just a bug in the program -and if so, any
ideas where to look?
Line 27. Whenever you got something not working, look around line 27, there
is *always* something wrong there.
I'll post followups if I figure it out.


And I hope you'll post followups to show us some code.
Jonathan
Jul 19 '05 #2
e
On Sun, 12 Oct 2003 22:34:13 -0400, "Jonathan Mcdougall"
<jo***************@DELyahoo.ca> wrote:

Please, try to post some code next time.

And I hope you'll post followups to show us some code.


Well, the app is quite large. I thought that someone might be able to
say offhand whether there was something wrong with the concept. In any
case, it looks like it's definitely a problem somewhere in the depths
of the program.. This is the file I'm using to test (well,
paraphrased):

----------------------------------------------------------------------------------------------------------

#include "log.h"
#include "logcontrol.h"
#include "logstream.h"

using namespace admin;
using namespace admin::log;

int main()
{
// Dummy
Logstream<int, std::ostringstream> info(ctl::NORMAL);

// Dummy reference..
Logstream<int, std::ostringstream> & r_info = info;

// Log object
// Log<> logger( /* Parameters */ );

// Tests
info << "This is some dummy text.. " << "More text.";

r_info << "This is some dummy text.. " << "More text.";

// logger.Read() << "Text." << "More text.";

return 0;
}
-----------------------------------------------------------------------------------------------------------

I did some more testing.. notice the commented-out line declaring the
Log object. This program compiles and runs as expected (I'm running
it in a debugger and I see the calls to operator<<), the Logstream
gathers the arguments both directly and via the reference. However, if
I uncomment the line declaring logger, both tests fail to execute. The
debugger shows that execution enters and skips the two first checks
and when it hits logger.Read(), it executes the member function (:)
and eventually returns a Logstream reference. The next thing that
happens is the return instruction (i.e. there are no calls to
operator<<.)

I'm stumped. A compiler problem? Let me know if you know of anything
that could possibly cause the problem in the language spec -I'll
gladly post additional code if necessary (please point out which part
you need it of), but I'd rather avoid it if possible.

Ta.
Jul 19 '05 #3
> I did some more testing.. notice the commented-out line declaring the
Log object. This program compiles and runs as expected (I'm running
it in a debugger and I see the calls to operator<<), the Logstream
gathers the arguments both directly and via the reference. However, if
I uncomment the line declaring logger, both tests fail to execute. The
debugger shows that execution enters and skips the two first checks
and when it hits logger.Read(), it executes the member function (:)
and eventually returns a Logstream reference. The next thing that
happens is the return instruction (i.e. there are no calls to
operator<<.)

I'm stumped. A compiler problem? Let me know if you know of anything
that could possibly cause the problem in the language spec
Who knows? You have the all code in front of you and you can't tell what
is wrong. The people in this group don't know what happens inside Log
and Logstream; how are they supposed to know what is wrong? Post minimal
code (preferably less than 50 lines), yet complete (that is
compilable), code that demonstrates the problem you are having. Chances
are that when trimming down your code to produce a example you will find
the problem.
-I'll
gladly post additional code if necessary (please point out which part
you need it of), but I'd rather avoid it if possible.


Without the right information nobody will be able to help you or even
make a wild guess.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 19 '05 #4
e
On Mon, 13 Oct 2003 12:53:46 +0200, "Peter van Merkerk"
<me*****@deadspam.com> wrote:
Who knows? You have the all code in front of you and you can't tell what
is wrong. The people in this group don't know what happens inside Log
and Logstream; how are they supposed to know what is wrong? Post minimal
code (preferably less than 50 lines), yet complete (that is
compilable), code that demonstrates the problem you are having. Chances
are that when trimming down your code to produce a example you will find
the problem.


Unfortunately that won't be possible. Looking at the code, there's
approximately 500 lines for a minimal compilable unit (lots of
dependencies), so it's not feasible to reproduce it as is. I'll see
what I can do.

I was just wondering if anyone knew how it would be possible for the
code to skip instructions because of the uncommented line -not crash,
or report an error, but skip instructions that it is not a part of
entirely. To me it seems that this is a problem with the compiler and
my code, not anything (legal) in the spec.

Thanks anyway!
Jul 19 '05 #5
> This program compiles and runs as expected (I'm running
it in a debugger and I see the calls to operator<<), the Logstream
gathers the arguments both directly and via the reference. However, if
I uncomment the line declaring logger, both tests fail to execute. The
debugger shows that execution enters and skips the two first checks
and when it hits logger.Read(), it executes the member function (:)
and eventually returns a Logstream reference. The next thing that
happens is the return instruction (i.e. there are no calls to
operator<<.)


If

int main()
{
a_logstream_object << "hey";
}

works (with a call to operator<<() ) and

Logstream &f()
{
return a_logstream_object;
}

int main()
{
f() << "hey";
}

does not (that is, operator<<() is not called _at all_), there is a serious
problem with the compiler. Change it.

If it does, back to your program, check the Read() member function. Does it
copy the Logstream? If yes, is it ok to copy it?

If not, try to access the Logstream object directly (it probably is a
private member of Logger; make it public and use it directly). Does that
Logstream object work? If not, the problem is there. If yes, the Read()
member function is the problem.

If the Logstream object, used directly, does not work, make sure it is
created and used correctly.

These are _very_ wild guesses, but that is the best I can do. Try to make
the code as complete as possible and post it on a website or something if
you really need help.
Jonathan
Jul 19 '05 #6
e
On Mon, 13 Oct 2003 15:50:51 -0400, "Jonathan Mcdougall"
<jo***************@DELyahoo.ca> wrote:

-- there is a serious
problem with the compiler. Change it.


I did. Intel compiles fine, as actually did g++ with certain (higher)
optimization levels. I rebuilt gcc and everything works now. I'll try
and find the problem and post in their bug lists :) Thanks, folks!
Jul 19 '05 #7

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

Similar topics

1
by: seller | last post by:
coded this method in bean. The intent was for the bean to run all the setters using the values in a vector argument. here's the code: public void loadSelf(Vector v) throws Exception { String...
5
by: Fernando Rodriguez | last post by:
Hi, I have a base class with a 'sanity-check' method. This method should iterate through all the methods and check if they are all 'thunks' (zero parameter functions, well actually, 1 parameter:...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
15
by: beliavsky | last post by:
What are the pros and cons of defining a method of a class versus defining a function that takes an instance of the class as an argument? In the example below, is it better to be able to write...
6
by: Manuel | last post by:
Can I invoke a function before main I could do it by invoking it in a Global object's constructor . Is there any other method other than this. Manuel
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
2
by: Jack | last post by:
I want to design a server which performs some business related user authentiation, and would like to let the clients invoke it through webservice, so my first thinking is using IIS as the web...
3
by: rickeringill | last post by:
Hi comp.lang.javascript, I'm throwing this in for discussion. First up I don't claim to be any sort of authority on the ecmascript language spec - in fact I'm a relative newb to these more...
18
by: Andrew Wan | last post by:
I have been developing web applications with ASP & Javascript for a long time. I have been using Visual Studio 2003.NET. While VS2003 is okay for intellisense of ASP & Javascript, it's still not...
26
by: tjhnson | last post by:
Hi, With properties, attributes and methods seem very similar. I was wondering what techniques people use to give clues to end users as to which 'things' are methods and which are attributes. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.