How to call a constructor, if I already declared it.

Okay so, I want to do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct FOO {
	int f;

	FOO (int ft) {
		f = ft;
	}

} jellyfoo ;

int main () {

	int x = something that is determined at runtime

	jellyfoo(x);

	return 0;
}


Of course, jellyfoo's constructor (the default (pretend there is one)) was already called, so I can't call the other one. I want to keep jellyfoo a global variable, but I don't know what x is. Do I HAVE to use a setter? Is there no way to do the Java thing that I know and love:

1
2
Foo jellyfish;
jellyfish = new Foo(x);


There's nothing like that in C++?

EDIT: And if I do have to use a setter, I know there is this handy initializing constructor thing:

 
FOO (int ft) : f(ft) {}


Or something like that. Is there anything like that that I can use withOUT it being a constructor? As in, just a regular function.
Last edited on
there is something like that! I'm just starting to get used to pointers so forgive me if this is wrong, but try this


1
2
3
Foo* jellyfish;

jellyfish = new Foo(x);


the first line creates a pointer called jellyfish that can store a memory for a Foo object.

the second line makes a new Foo(x) object and stores it in jellyfish.

whenever you make a new anything though, it MUST have a matching delete.

somewhere in your code your going to need
 
delete jellyfish;


Last edited on
Thanks for the reply! But that's dynamic allocation, right? I'm not sure if that's exactly what I'm looking for. I mean, I think it works, but the only thing I don't know right away is 'x'. I think it would be better to use a setter then. Someone correct me if I'm wrong though.

EDIT: Just to clarify a little bit, I am against using a setter, because I am only going to set 'f' once in the entire program, and that is why it would be nice to have it in the constructor. However, I want other functions to be able to use "jellyfish", not just main().
Last edited on
Is there anything like that that I can use withOUT it being a constructor? As in, just a regular function.

You've answered your own question on this one :)

bpx95 wrote:
use a setter
not sure what a 'setter' is, but if you mean x is inputted by the user you could do
1
2
3
4
5
6
std::cout << "Enter a number: ";
std:: cin >> x;

Foo* jellyfish;

jellyfish = new Foo(x);


This will show the words "Enter a number: " and then wait for the user to input a number that will be stored in x.
Yeah, I just don't want to have to use a setter, because I only need to set 'f' once in the entire program, and I would prefer to set it in the constructor. Maybe it's just me being picky about encapsulation and all that.

EDIT: To poster above, a setter is like this:

1
2
3
4
5
6
7
struct Foo{
     int f;

     void setF(int ft){
           f = ft;
     }
}
Last edited on
You can't just call the constructor, the constructor is called specifically when the object is created (and thus is the only thing that can initialize const vars and other things). The closest you could get is to make an init/reset function and call that.
1
2
Foo jellyfish;
jellyfish = new Foo(x);


You want the above Java to be same as C++ you just change abit.

1
2
3
4
5
6
7
8
9
10
11
12
13
class FOO {
	int f;

	FOO (int ft) {
		f = ft;
	}

};

int x = something that is determined at runtime
Foo* jellyfish = new Foo(x);
...
delete jellyfish;
Last edited on
Topic archived. No new replies allowed.