How to get parent class' pointer

Discussion in 'C++' started by Paul, Aug 11, 2003.

  1. Paul

    Paul Guest

    Hi all,
    Here is what I am trying to do. I have a parent class calling a child
    class. when one function in child class is called, i need to call
    parent class' function. How can I get parent class' pointer?
    Thx
    Paul
     
    Paul, Aug 11, 2003
    #1
    1. Advertising

  2. "Paul" <> wrote...
    > Here is what I am trying to do. I have a parent class calling a child
    > class. when one function in child class is called, i need to call
    > parent class' function. How can I get parent class' pointer?


    First of all, try to understand that you're not in Java any more.

    There is no concept of "parent class" in C++. If you're talking
    of a nested class and enclosing class, then they are unrelated.
    In the code:

    struct Enclosing {
    struct Nested {
    void foo();
    };
    };

    an object of type Enclosing does not contain a subobject Nested.
    The relationship between them is purely descriptional. In the
    code:

    struct Enclosing {
    struct Nested {
    void foo();
    };

    Nested nested;

    void bar();
    };

    'nested' is a data member of type Nested in an Enclosing object.
    If, when executing Enclosing::bar(), you need to execute the
    Nested::foo, and in that function you need to get to the object
    of type Enclosing that called it, simply pass it as an argument:

    void Enclosing::bar()
    {
    nested.foo(this);
    }

    [of course, 'Nested::foo' has to be corrected to accept one
    argument, of type Enclosing* :

    struct Enclosing {
    struct Nested {
    void foo(Enclosing*);
    };

    Nested nested;

    void bar();
    };

    Victor
     
    Victor Bazarov, Aug 11, 2003
    #2
    1. Advertising

  3. Victor,

    I am moving from VB to C++, and I am just starting to learn about C++
    Classes. I am not sure I understand the following:

    > In the code:
    > struct Enclosing {
    > struct Nested {
    > void foo();
    > };
    > };
    >
    > an object of type Enclosing does not contain a subobject Nested.
    > The relationship between them is purely descriptional.


    So, this does _not_ mean that Nested is in the scope of Enclosing? So from
    global/file scope (not from inside the class Enclosing), how would you
    declare a variable of type Nested?

    Enclosing::Nested nested;
    or
    Nested nested;

    or is it unavailable?

    Thanks,
    Jeremy
     
    Jeremy Cowles, Aug 11, 2003
    #3
  4. "Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote...
    > I am moving from VB to C++, and I am just starting to learn about C++
    > Classes.


    Sorry about my note on Java, then. There is a significant difference
    in nested classes' implementation between those two. I suppose that
    between VB and C++ there might be a similar one.

    > I am not sure I understand the following:
    >
    > > In the code:
    > > struct Enclosing {
    > > struct Nested {
    > > void foo();
    > > };
    > > };
    > >
    > > an object of type Enclosing does not contain a subobject Nested.
    > > The relationship between them is purely descriptional.

    >
    > So, this does _not_ mean that Nested is in the scope of Enclosing?


    In scope, yes. However, unlike in Java (and I don't know about VB),
    when you create an instance of 'Enclosing', C++ doesn't automatically
    create an instance of 'Nested'.

    > So from
    > global/file scope (not from inside the class Enclosing), how would you
    > declare a variable of type Nested?
    >
    > Enclosing::Nested nested;


    Sure. Qualified name should work anywhere.

    > or
    > Nested nested;


    Yes, if you're inside the scope of 'Enclosing' class.

    >
    > or is it unavailable?


    It's available alright. I described the difference above.

    Victor
     
    Victor Bazarov, Aug 11, 2003
    #4
  5. "Mike Smith" <> wrote...
    > Victor Bazarov wrote:
    >
    > > "Paul" <> wrote...
    > >
    > >>Here is what I am trying to do. I have a parent class calling a child
    > >>class. when one function in child class is called, i need to call
    > >>parent class' function. How can I get parent class' pointer?

    > >
    > >
    > > First of all, try to understand that you're not in Java any more.
    > >
    > > There is no concept of "parent class" in C++. If you're talking
    > > of a nested class and enclosing class, then they are unrelated.

    >
    > ICBW but I got the impression that what he's calling "parent" and
    > "child" would translate into "base" and "derived" in C++, not
    > "enclosing" and "nested".


    I agree that "base-derived" is a more commonly accepted meaning of
    "parent-child" than "enclosing-nested", however, there are others:
    in a tree a node closer to the root is often called 'a parent', in
    a process communication the one that initiates the other is a parent,
    the initiated is the child, in windows-driven UI, windows are often
    placed in a tree-like hierarchy as well, perhaps since there exist
    so many different applications of the terms "parent-child", the C++
    language definition doesn't make any use of it.

    Victor
     
    Victor Bazarov, Aug 11, 2003
    #5
  6. "Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote...
    > [...]
    > Just FYI, VB does not automatically create an instance for you, and based

    on
    > what you said, VB works the same in terms of scope as well.


    Good info. Off-topic, but good info, thanks.
     
    Victor Bazarov, Aug 11, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tirath
    Replies:
    3
    Views:
    719
    Ivan Vecerina
    Oct 12, 2003
  2. Alberto Luaces
    Replies:
    6
    Views:
    396
    Fraser Ross
    Jun 19, 2006
  3. metal
    Replies:
    8
    Views:
    519
    Bruno Desthuilliers
    Oct 30, 2009
  4. , India
    Replies:
    8
    Views:
    980
    gwowen
    Aug 18, 2010
  5. encapsul

    how to get child class "name" from parent class

    encapsul, Sep 15, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    406
    Brock Allen
    Sep 15, 2005
Loading...

Share This Page