First of all, that is not an example that could be compiled. Yes, you have errors, but you have also bits that distract from those errors.
For example, the
....
. That is not valid C++ and you clearly do not even intent it to be valid.
Then, the
Base
is actually a class derived from
class Source
. You don't show code of that class, so we could never succeed, even if your code were correct.
Last, there is no
main()
function that would use these types.
Lets cut some out:
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 35 36 37 38 39 40 41
|
class Base
{
public:
Base() {}
virtual ~Base() {}
virtual void fun( double member1 );
private:
double member_1;
};
class Derived : public Base
{
public:
double aa = 1.2;
Derived() {}
virtual ~Derived() {}
void fun( double member1 );
};
void Base::fun( double member1 )
{
Base* bb = new Base;
Derived* dd = new Derived;
bb->fun( double member1 ) // error: expected primary-expression before 'double'
member_1 = bb->member1;
dd->fun( double member1 ) // error: expected primary-expression before 'double'
member_1 = dd->member1;
}
void Derived::fun( double member1 )
{
member1 = aa;
}
int main()
{
}
| |
The
bb->fun( double member1 )
makes no sense.
It has no semicolon at end. The statement that you have here is actually:
bb->fun( double member1 ) member_1 = bb->member1 ;
Which makes even less sense.
If you want to call a function, then you must supply
a value, not
a declaration as
argument (and must have semicolon at right place):
bb->fun( 3.14 ) ;
In the simplified example here, the
Base
object has exactly one data member:
double member_1
The Derived object has two members: the
double aa
and also the
double member_1
, because it inherits
Base
.
The
Derived::fun()
has parameter
double member1
but that variable is local to the function. Effectively, the Derived::fun() does absolutely nothing.
You can call the
Base::fun()
only if you already have a Base object. Why do you create a second Base and one Derived objects within the function (and never deallocate those dynamically allocated objects)?
If your Base::fun() creates a Base object and calls Base::fun(), the second call will create a Base base and call Base::fun(), which will create a Base base and call Base::fun(), which will create a Base base and call Base::fun(), which will create a Base base and call Base::fun(), which will create a Base base and call Base::fun(), ... until computer runs out of memory.