I have a different example , I wonder how can I use teh Initializer.
class A
{
public :
A(const B& b) ;
private:
const int x;
const int y;
void func1(B& b);
void func2(B& b);
}
A::A(const B&b) {
// this will not compile since this is not done in the initializer.
x = func1(b);
y = func2(b);
}
Is there a work around? of course, I can remove 'const' in x, y.
But I wonder if there is a better solution.
Well none of this will work.
What do you want to achieve?
func1 and func2 do not return a value, so you can't use the return value
to initialise an int. Do func1 and func2 modify b? If not, use const,
otherwise remove the const from constructor of A.
Anyway, you can call a function in the initialiser list, consider:
class B;
class A {
public :
A(const B& b);
private:
const int x;
const int y;
int func1(const B& b);
int func2(const B& b);
};
A::A(const B& b) : x(func1(b)), y(func2(b)) {
}
With appropriate missing pieces filled in, of course.
Ben Pope