B
bardos
Hello, I'm relatively new to C++ so I hope you can help clear up a
problem. You may well be able to do so before checking the example, so
don't be put off by the long post The problem regards the usage and
effect of the :: scope qualifier. I'm converting a Java app to C++ and
need to find the equivalent of using the Java 'super' keyword. This
executes the 'superclass's constructor (with possibly different
arguments from the derived constructor). I thought I would do this by
using the C++ :: operator, to pick the required base constructor from
within the derived class. Eg,
DerivedClass:erivedClass(int x)
{
BaseClass::BaseClass(x , NULL, NULL); // Call different
constructor.
}
I was hoping this would set the values in the derived class using the
base constructor.
This compiles fine, but any operation carried out in the base class
seem to have no effect on the dervied class. Does anyone know why this
is, and how to remedy this? I've enclosed an example.
The program has 3 classes, base1, base 2 (inherits base 1) and derived
(inherits base2). Each class has one int (b1,b2 and d respectively).
Using :: to call methods to set values b1 and b2 in base1 and base2
has no effect, leaving the values undefines inthe derived class.
The program outputs -842150451 -842150451 3 for the 3 values, instead
of the expected 1,2,3.
I would really appretiate any help with my misunderstanding,
Regards,
Steve.
// EXAMPLE CODE.
// Base 1 declaration.
class base1
{
public:
base1(){}; // Default constructor.
base1(int);
int b1;
};
base1::base1(int i)
{
b1 = i;
}
// Base 2 declaration, uses base1 as base class.
class base2 ublic base1
{
public:
base2(){}; // Default constructor.
base2(int);
int b2;
};
base2::base2(int i)
{
b2 = i;
base1::base1(1);
}
// Derived class, uses base2 (and therefore class1) as base classes.
class derived : public base2
{
public:
derived(int);
int d;
};
derived::derived(int i)
{
d = i;
base2::base2(2);
}
void main(int argc,char *argv[])
{
derived *d;
d = new derived(3);
printf("%d %d %d ",d->b1,d->b2,d->d);
}
problem. You may well be able to do so before checking the example, so
don't be put off by the long post The problem regards the usage and
effect of the :: scope qualifier. I'm converting a Java app to C++ and
need to find the equivalent of using the Java 'super' keyword. This
executes the 'superclass's constructor (with possibly different
arguments from the derived constructor). I thought I would do this by
using the C++ :: operator, to pick the required base constructor from
within the derived class. Eg,
DerivedClass:erivedClass(int x)
{
BaseClass::BaseClass(x , NULL, NULL); // Call different
constructor.
}
I was hoping this would set the values in the derived class using the
base constructor.
This compiles fine, but any operation carried out in the base class
seem to have no effect on the dervied class. Does anyone know why this
is, and how to remedy this? I've enclosed an example.
The program has 3 classes, base1, base 2 (inherits base 1) and derived
(inherits base2). Each class has one int (b1,b2 and d respectively).
Using :: to call methods to set values b1 and b2 in base1 and base2
has no effect, leaving the values undefines inthe derived class.
The program outputs -842150451 -842150451 3 for the 3 values, instead
of the expected 1,2,3.
I would really appretiate any help with my misunderstanding,
Regards,
Steve.
// EXAMPLE CODE.
// Base 1 declaration.
class base1
{
public:
base1(){}; // Default constructor.
base1(int);
int b1;
};
base1::base1(int i)
{
b1 = i;
}
// Base 2 declaration, uses base1 as base class.
class base2 ublic base1
{
public:
base2(){}; // Default constructor.
base2(int);
int b2;
};
base2::base2(int i)
{
b2 = i;
base1::base1(1);
}
// Derived class, uses base2 (and therefore class1) as base classes.
class derived : public base2
{
public:
derived(int);
int d;
};
derived::derived(int i)
{
d = i;
base2::base2(2);
}
void main(int argc,char *argv[])
{
derived *d;
d = new derived(3);
printf("%d %d %d ",d->b1,d->b2,d->d);
}