Using a function which returns an object

S

singhal.prateek

Hi,

In a class, I have a static function which returns a particular object
of the class depending on the arguments. Now, if I use this function to
call another method of the class, I get an error. It is like:

printf("%s", getName( getObject(params) ) ); // Works fine!
getObject(params).read(); // Gives read undefined error.

read is a function of the class in consideration here.

Any clues? Is this allowed at all in C++?

Thanks,
Prateek
 
A

Alf P. Steinbach

* (e-mail address removed):
In a class, I have a static function which returns a particular object
of the class depending on the arguments. Now, if I use this function to
call another method of the class, I get an error. It is like:

printf("%s", getName( getObject(params) ) ); // Works fine!
getObject(params).read(); // Gives read undefined error.

read is a function of the class in consideration here.

Any clues?

"read" is undefined.

Is this allowed at all in C++?

Yes.
 
R

Rolf Magnus

Alf said:
* (e-mail address removed):

"read" is undefined.



Yes.

What Alf wants to say here is that you (the OP) didn't provide enough
information to give a more helpful answer. Post a minimal, but complete
program that shows the problem you observed. (If I only got one Euro for
each time I write that sentence...)
 
S

singhal.prateek

Thanks for the replies. Posting a complete program is difficult but
this one should be able to give an idea.

class Test {
getObject(char * param) {
/* code here */
return some-object;
}

void read() {
printf("Hullow!!");
}
}

main() {
Test ob1;
Test ob2;
/*
* Code Here
*
*/
getObject("hi").read(); // I get an error here
}

Hope I made it clear now.

Thanks,
Prateek
}
 
A

Andre Kostur

Thanks for the replies. Posting a complete program is difficult but this
one should be able to give an idea.

It should be complete and _compilable_. This is not:
class Test {
getObject(char * param) {

Returning what type?
/* code here */
return some-object;

some minus object? We can't even see the definitions for "some" and
"object", so who knows what applying operator-() to them will do.
}

void read() {
printf("Hullow!!");
}
}

This certainly isn't compilable. No semicolon at the end of the class.

Where the return type for this too?
Test ob1;
Test ob2;
/*
* Code Here
*
*/
getObject("hi").read(); // I get an error here

Because your code is broken.
}

Hope I made it clear now.

Very. Post a minimal, complete and compilable code example showing your
problem. (Which I'd guess is that you don't ever declare return types
from functions).
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi,

In a class, I have a static function which returns a particular object
of the class depending on the arguments. Now, if I use this function to
call another method of the class, I get an error. It is like:

In addition to what Alf, Rolf and Andre asked you to include, the whole
error message might also be of help.
printf("%s", getName( getObject(params) ) ); // Works fine!
getObject(params).read(); // Gives read undefined error.

A quick guess would be that getObject() returns a pointer in which case
'->' should be used instead of '.'.

Erik Wikström
 
C

Cy Edmunds

[snip]

Post a minimal, but complete
program that shows the problem you observed. (If I only got one Euro for
each time I write that sentence...)

The funny thing is that if anybody ever heeded that advice they would almost
certainly find the problem themselves. But they never do.

Cy
 
J

JustBoo

In a class, I have a static function which returns a particular object
of the class depending on the arguments. Now, if I use this function to
call another method of the class, I get an error. It is like:
Any clues? Is this allowed at all in C++?
Thanks,
Prateek

Disclaimer: I do not give a damn how politically correct my response
is. If you don't like it, move on.

I find it quite disturbing that a person who has most likely taken a
job that would have been filled by someone in the western world now
comes into a newsgroup populated mostly by westerners and lazily asks
them to do his job. Murphy's law says the guy who helps him is an
unemployed programmer who is bored.

The ultimate irony - if not out-right insult, is the American people
are funding their own economic demise. The American government pays
companies (tax-credits) to export our high-tech jobs. So it is NOT an
even playing field. It is NOT capital following cheap labor.

"Don't worry about the west, they will sell us the rope we will hang
them with." - attributed to various despotic communist leaders.
 
R

raylopez99

JustBoo said:
Disclaimer: I do not give a damn how politically correct my response
is. If you don't like it, move on.

I find it quite disturbing that a person who has most likely taken a
job that would have been filled by someone in the western world now
comes into a newsgroup populated mostly by westerners and lazily asks
them to do his job. Murphy's law says the guy who helps him is an
unemployed programmer who is bored.

And your problem with that is, sir, what?
The ultimate irony - if not out-right insult, is the American people
are funding their own economic demise. The American government pays
companies (tax-credits) to export our high-tech jobs. So it is NOT an
even playing field. It is NOT capital following cheap labor.

Why not? Sounds like you need to be reeducated--in a Marxist country.
"Don't worry about the west, they will sell us the rope we will hang
them with." - attributed to various despotic communist leaders.

And they failed.

BTW, I think the problem with the guy's code was he did not have a copy
constructor... when you return an object you must have a copy
constructor I recall from my never used textbook on OOP.

RL
 
D

Diego Martins

JustBoo said:
Disclaimer: I do not give a damn how politically correct my response
is. If you don't like it, move on.

I find it quite disturbing that a person who has most likely taken a
job that would have been filled by someone in the western world now
comes into a newsgroup populated mostly by westerners and lazily asks
them to do his job. Murphy's law says the guy who helps him is an
unemployed programmer who is bored.

The ultimate irony - if not out-right insult, is the American people
are funding their own economic demise. The American government pays
companies (tax-credits) to export our high-tech jobs. So it is NOT an
even playing field. It is NOT capital following cheap labor.

"Don't worry about the west, they will sell us the rope we will hang
them with." - attributed to various despotic communist leaders.

har har har har :)

this is one of bests troll posts I've seen ever :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top