call to a constructor with a the same name of a local variable

In the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class f {
public:
	int *a;
	f(int g) {
		a = (int *) malloc (sizeof(int));
		*a = g;
	}
};

class h : public f {
	int f;
	h(int g) :
		f(g) {} // the compiler interprets this line as an assignation to the local variable f
};



I can't construct a instance of f cause I can't find a way to call to the f's constructor. Is there a way to do that?

Thank you! (sorry for my bad english I am Belgian)
try f::f(g) {}
Last edited on
You shouldn't have a data member using the same identifier as the class. Usually naming conventions will make sure that doesn't happen (CamelCase for class identifiers, mixedCase or lowercase or lower_case_with_underscores for functions and data, for instance).
It doesn't work, the reported error is:
error C2039: '{ctor}' : is not a member of 'f'

:(
Ok filipe, so it's not possible. Thank you!
I'm not saying it's not possible (I really don't know), I'm just saying it's confusing and there are good ways to prevent it.
Agree with what Filipe says - Better names is what is really required.
I agree with you, but I ask for that because I gonna have an c++ exam this Tuesday, and the questions are gonna be this kinds of fuckings paranoias xD
anyway, thank you for your help :)
Topic archived. No new replies allowed.