Address of an object

Discussion in 'C++' started by raan, Aug 1, 2007.

  1. raan

    raan Guest

    Please see the following program. I uses Windows XP/VS2003 as my
    environment.

    I have two classes inherited from a base class called Interfaces. I
    have a class named General in which I have instantiated objects for A
    and B. I have instantiated two objects ga, gb of type A and B
    respectively in my main function. I wish ga and gb, in my main,
    points to the same address space that a and b occupies.

    My ultimate aim is to have () operator overloaded in class A and class
    B so that I can call ga() and gb() (functors) from my main program.
    Basically the main program will be replaced by another class and
    should use the objects intantiated in General class to carry out its
    functionality. Calling it as (*ga)() and (*gb)() doesn't look cool .
    Hope you guys get the point, why I want it this way.

    Is there anyway I can achieve this


    #include "stdafx.h"
    #include <iostream>

    using namespace std;

    class Interfaces
    {
    public:
    Interfaces(){}
    ~Interfaces(){}
    };

    class A : public Interfaces
    {
    public:
    A() {};
    ~A() {};
    void afunc()
    {
    cout << this << endl;
    cout << "a func called" << endl;
    }

    };

    class B: public Interfaces
    {
    public:
    B() {};
    ~B() {};
    void bfunc()
    {
    cout << this << endl;
    cout << "b func called" << endl;
    }
    };

    class General
    {
    private:
    A a;
    B b;
    public:
    General() {cout << "A Addres " << &a << endl;
    cout << "B Addres " << &b <<endl;};
    ~General() {};
    void QueryInterface(int id, Interfaces *ptr)
    {
    switch(id)
    {
    case 1:
    ptr = &a;
    cout << "Ptr a "<< ptr << endl;
    break;
    case 2:
    ptr = &b;
    cout << "Ptr b "<< ptr << endl;
    break;
    }
    }
    };

    int _tmain(int argc, _TCHAR* argv[])
    {
    General *g = new General;
    A ga;
    B gb;

    cout << "Before QI ga " << &ga << endl;
    cout << "Before QI gb " << &gb << endl;

    g->QueryInterface(1, &ga);
    g->QueryInterface(2, &gb);

    cout << "After QI ga " << &ga << endl;
    cout << "After QI gb " << &gb << endl;

    ga.afunc();
    gb.bfunc();

    getchar();

    return 0;
    }
    raan, Aug 1, 2007
    #1
    1. Advertising

  2. raan

    Daniel T. Guest

    raan <> wrote:

    > Please see the following program. I uses Windows XP/VS2003 as my
    > environment.
    >
    > I have two classes inherited from a base class called Interfaces. I
    > have a class named General in which I have instantiated objects for A
    > and B. I have instantiated two objects ga, gb of type A and B
    > respectively in my main function. I wish ga and gb, in my main,
    > points to the same address space that a and b occupies.


    Can't be done. 'ga' and 'gb' are not pointers, so you can't change what
    they point to.

    > My ultimate aim is to have () operator overloaded in class A and class
    > B so that I can call ga() and gb() (functors) from my main program.
    > Basically the main program will be replaced by another class and
    > should use the objects intantiated in General class to carry out its
    > functionality. Calling it as (*ga)() and (*gb)() doesn't look cool .
    > Hope you guys get the point, why I want it this way.


    You can create a wrapper for that, but frankly there is probably a
    better way to do what you are trying to do:

    class Interface {
    public:
    virtual ~Interface() { }
    virtual void invoke() = 0;
    };

    class Wrapper {
    Interface* a;
    public:
    Wrapper(): a( 0 ) { }
    void set( Interface* i ) {
    a = i;
    }
    void operator()() {
    if ( a )
    a->invoke();
    }
    };

    class A : public Interface {
    public:
    void invoke() { cout << "invoke A\n"; }
    };

    class B : public Interface {
    public:
    void invoke() { cout << "invoke B\n"; }
    };

    int main() {
    A a;
    B b;
    Wrapper wrap;

    wrap.set( &a );
    wrap();

    wrap.set( &b );
    wrap();
    }
    Daniel T., Aug 1, 2007
    #2
    1. Advertising

  3. * raan:
    >
    > #include "stdafx.h"


    This header is not standard C++.


    > #include <iostream>
    >
    > using namespace std;
    >
    > class Interfaces
    > {
    > public:
    > Interfaces(){}
    > ~Interfaces(){}
    > };


    This class serves no useful purpose, and in fact, you're not using it.


    > class A : public Interfaces
    > {
    > public:
    > A() {};
    > ~A() {};
    > void afunc()
    > {
    > cout << this << endl;
    > cout << "a func called" << endl;
    > }
    >
    > };
    >
    > class B: public Interfaces
    > {
    > public:
    > B() {};
    > ~B() {};
    > void bfunc()
    > {
    > cout << this << endl;
    > cout << "b func called" << endl;
    > }
    > };


    These two classes have nothing in common (except the unused Interface
    base class).


    > class General
    > {
    > private:
    > A a;
    > B b;
    > public:
    > General() {cout << "A Addres " << &a << endl;
    > cout << "B Addres " << &b <<endl;};
    > ~General() {};
    > void QueryInterface(int id, Interfaces *ptr)
    > {
    > switch(id)
    > {
    > case 1:
    > ptr = &a;
    > cout << "Ptr a "<< ptr << endl;
    > break;
    > case 2:
    > ptr = &b;
    > cout << "Ptr b "<< ptr << endl;
    > break;
    > }
    > }
    > };


    The QueryInterfaces function reports only whether you passed in 1 or 2
    as argument, what's the use of that?


    > int _tmain(int argc, _TCHAR* argv[])


    This is not standard C++. In standard C++ use 'main'. Like

    int main()


    > {
    > General *g = new General;
    > A ga;
    > B gb;
    >
    > cout << "Before QI ga " << &ga << endl;
    > cout << "Before QI gb " << &gb << endl;
    >
    > g->QueryInterface(1, &ga);
    > g->QueryInterface(2, &gb);
    >
    > cout << "After QI ga " << &ga << endl;
    > cout << "After QI gb " << &gb << endl;
    >
    > ga.afunc();
    > gb.bfunc();
    >
    > getchar();
    >
    > return 0;
    > }



    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 1, 2007
    #3
    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. avnrao
    Replies:
    1
    Views:
    7,210
    avnrao
    May 4, 2004
  2. Patrice
    Replies:
    0
    Views:
    877
    Patrice
    May 4, 2004
  3. Raterus
    Replies:
    0
    Views:
    428
    Raterus
    Jun 2, 2004
  4. Alex Stevens
    Replies:
    0
    Views:
    768
    Alex Stevens
    Aug 10, 2004
  5. =?Utf-8?B?QW5nZWw=?=

    Pass a Web Address and return a File System Address

    =?Utf-8?B?QW5nZWw=?=, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    529
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=
    Sep 21, 2005
Loading...

Share This Page