Click to See Complete Forum and Search --> : access to a timer variable in base class from a non-derived class


Moore
May 13th, 2005, 05:15 AM
hi, im very new to c++, so any guidance is welcomed.
I'm wondering is it possible to use a variable ( which is a public member of a base class) from a class that is not derived from that base.

my understanding is that public members are accessible by all functions in a program but is this limited to derived classes from the base?

if so is it possible to get around this and what would be the best way.

I have seen the use of friend class which looks like it allows the friend class to access the private members of another class.
Is there something similar for public members.

i get the following type error when compiling the file by trying to use the variable in the following way from the non-derived class.
my_base_class::variable_name

Error : type `my base class' is not a base type for type 'class where I want to use variable_name'

when I just use the variable name directly, the file builds but the program fails with:
Error : undefined reference to `variable_name'

I'd appreciate any help.

thank you.

Siddhartha
May 13th, 2005, 05:20 AM
Post code.
You can type cast the instance of the derived class, to get an Instance of the base class.
Do pBase->m_YourVariable to use it.

Moore
May 13th, 2005, 06:10 AM
but if the class i want to use the variable in is not derived from the class where the variable is a public member, then will this still work.

what would the syntax for such an operation include.

darwen
May 13th, 2005, 07:07 AM
How about this :


class A
{
public:
int GetValue() const { return m_nValue; }
void SetValue(int nValue) { m_nValue = nValue; }

private:
int m_nValue;
} ;

class B
{
public:
void Function()
{
int nValue = m_a.GetValue();
m_a.SetValue(nValue);
}

private:
A m_a;
} ;


Does this explain it ?

You DO have to have access to the instance of class A though : i.e. either -


A a; // value
A *pA = new A(); // pointer
A &rA = a; // reference

int nValue = 0;
nValue = a.GetValue();
nValue = pA->GetValue();
nVAlue = rA.GetValue();


All classes have instances : i.e. they can be created multiple times. You have to have access to an instance to call the method (unless it's static of course but that's another subject...).

Darwen.

Moore
May 13th, 2005, 09:54 AM
Darwen

thank you.

so if i update the variable in class B by using the instance of class A, will the updated variable be available to class A.

ie. class A has a timer value.
I access timer value and modify value in class B.
Will class A have the new value?

thanks.

darwen
May 13th, 2005, 10:25 AM
Yes. Providing that class A hasn't been passed into class B by value. You can check this by declaring a private copy constructor on class A i.e.


class A
{
private:
A(const A &rA) { };
} ;


And if you explicitly Set the variable in A (by calling something like SetValue from above).

Darwen.

RoboTact
May 13th, 2005, 11:19 AM
Read the comments in the following code. When you first wrote my_base_class::variable_name, compiler thought it's this 3rd option in my example (with value 12), and found that my_base_class isn't a base class of this class which it required in that option and printed it as an error, while you meant the 2nd option (with value 11), variable of instance of another class.

When you changed the code, compiler thought it's the 5th option (with value 14), global variable, and found that there is no global variable with this name (it's strange that it wasn't font at one, it seems that it allows non-declarated external symbols). #include <iostream>

using namespace std;

class A
{
public:
int var;
static int s_var;
};

class B
{
public:
static int var;
};

int B::var; // this is an instance of static variable of class B

int var; // global variable called var

class C
{
public:
int var;
};

class D:public C
{
public:
int var;
void funct()
{
var=10; // access this->var, of this instance of class D
A a;
a.var=11; // access a.var, of instance a of class A
C::var=12; // access this->C::var, that is var of base class C of this class, in this instance
B::var=13; // access static variable of class B, that is no-instance field shared for class B
::var=14; // access global variable called var

// let's print them to ensure they are different ones.
cout<<"var="<<var<<endl;
cout<<"a.var="<<a.var<<endl;
cout<<"C::var="<<C::var<<endl;
cout<<"B::var="<<B::var<<endl;
cout<<"::var="<<::var<<endl;
}
};

int main(int argc,char **argv)
{
D d;
d.funct();
return 0;
}