CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2005
    Posts
    112

    Question access to a timer variable in base class from a non-derived class

    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.

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Thumbs up Re: access to a timer variable in base class from a non-derived class

    1. Post code.
    2. You can type cast the instance of the derived class, to get an Instance of the base class.
    3. Do pBase->m_YourVariable to use it.

  3. #3
    Join Date
    May 2005
    Posts
    112

    Re: access to a timer variable in base class from a non-derived class

    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.

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: access to a timer variable in base class from a non-derived class

    How about this :

    Code:
    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 -

    Code:
    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    May 2005
    Posts
    112

    Re: access to a timer variable in base class from a non-derived class

    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.

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: access to a timer variable in base class from a non-derived class

    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.

    Code:
    class A
    {
    private:
        A(const A &rA) { };
    } ;
    And if you explicitly Set the variable in A (by calling something like SetValue from above).

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: access to a timer variable in base class from a non-derived class

    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).
    Code:
    #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;
    }
    "Programs must be written for people to read, and only incidentally for machines to execute."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured