Return Type

L

LEF

Hello All,

My apologies if this question is in the FAQ, however, I looked for the FAQ
but couldn't find it. Direction to it would be appreciated. My question is
very simple, can a class have as a return type for one it's methods the type
the class defines? Probably not clear, an example:

class example {
public:
example testfn();
};

example example::testfn() {
}

Is that valid?

Thanks.
 
V

Victor Bazarov

LEF said:
My apologies if this question is in the FAQ, however, I looked for the FAQ
but couldn't find it. Direction to it would be appreciated. My question is
very simple, can a class have as a return type for one it's methods the type
the class defines? Probably not clear, an example:

class example {
public:
example testfn();
};

example example::testfn() {
}

Is that valid?

Absolutely. Do you experience any problems?

V
 
C

Chris \( Val \)

| Hello All,
|
| My apologies if this question is in the FAQ, however, I looked for the FAQ
| but couldn't find it. Direction to it would be appreciated. My question is
| very simple, can a class have as a return type for one it's methods the type
| the class defines? Probably not clear, an example:
|
| class example {
| public:
| example testfn();
| };
|
| example example::testfn() {
| }
|
| Is that valid?

As long as you actually return an object of type
'example', then yes - Not only is it valid, it is
a commonly used construct.

Cheers,
Chris Val
 
S

Samee Zahur

That IS how it is mostly done. But to be able to return an object of
type 'example' from any function (member or not) by value, you must
have a valid copy constructor (explicitly defined or compiler
generated). If you don't know what a copy-ctor is, forget what I just
said :)

Samee
 

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
474,294
Messages
2,571,507
Members
48,193
Latest member
DannyRober

Latest Threads

Top