function return class handle

First off I'm very new to c++. now what I cannot find anywhere is the ability to declare a variable of type class so that it can receive an object reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream> 
using namespace std;

class foo {
public:
	int a;
	foo ();
};
foo::foo() {
	a = 231;	// just a random number.
}

foo make_an_instance_foo(){
	foo g;
	return g;
}
int main(){
    //foo f; works but I want the below to work too
	f = make_an_instance_foo();
	
	cout << f.a << endl;
	system("PAUSE");
}


I know its a simple question but i cannot find anything on this. How would I declare f in 'f = make_an_instance_foo();' Please help thanks.
Last edited on
Ah, so you want to be able to create new foo variables on run-time? C++ directly provides a mechanism for that. It is called dynamic memory allocation and you can do it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

class foo
{
public:
    int a;
    foo ();
};

foo::foo()
{
    //just a random number.
    a = 231;
}

int main()
{
    //this is a foo pointer
    foo * pfoo;

    //create a new instance!
    pfoo=new foo;

    //access the foo instance
    //through the foo pointer
    cout << (*pfoo).a << endl;

    //kill the instance
    delete pfoo;
    
    system("PAUSE");
    return 0;
}

Useful links:

http://cplusplus.com/doc/tutorial/pointers/
http://cplusplus.com/doc/tutorial/dynamic/
Last edited on
Ahh so then this would also work. Thanks m4ster r0shi.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream> 
using namespace std;

class foo {
public:
	int a;
	foo ();
};
foo::foo() {
// just a random number.       
	a = 231;
}

foo make_an_instance_foo(){
	foo g;
	g.a = 999;
	return g;
}
int main(){
    // use f as foo referance?
	foo f = make_an_instance_foo();
	
	cout << f.a << endl;
	system("PAUSE");
}


But does this mean that 2 instances are created(one of them being lost right away) or just 1?
Last edited on
I think what you want to do is define a copy constructor explicitly.
Musiciankool wrote:
But does this mean that 2 instances are created(one of them being lost right away) or just 1?

Good question. The answer is that 2 instances are created, one being g and one being f, and g is lost after being copied to f. Also, notice that this doesn't allocate memory on run-time. f is created on compile-time.
Ah... When I first answered I only took a glance at the code you posted, but now that I see this:

Musiciankool wrote:
...so that it can receive an object reference.

I guess moorecm is right. In that case this should help:

http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/
so Then to dynamically create and delete many instances of the same class during run-time I have to use pointer assignment ,to an array? otherwise they are lost or basically stuck in memory without a way to access them? Can making lots of instances be done with an array of references or just pointer's ( just for simplicity on the project I'm working on, references seem easier to handle. or rather easier to translate to)?

like: (not exactly sure how to make an array yet but just in general)

array fooArray(100) //but the array can be re-sized during run-time
for (int i = 0;i < 100; i++){
foo fooArray(i);
}

Last edited on
So, you actually want dynamic memory allocation? Well, then, yes, single instances can be created like I demonstrated above.

For multiple instances, well, we have two options.

If you want these instances to be created all at once, you can still use new/delete but with a slightly different syntax. Check here:

http://cplusplus.com/doc/tutorial/dynamic/

If you want these instances to be created one at a time, then you should use a stl container
(vector is a good choice to start experimenting with):

http://cplusplus.com/reference/stl/
http://cplusplus.com/reference/stl/vector/
Last edited on
Thanks this helps a lot!
Topic archived. No new replies allowed.