Type of containing class

Discussion in 'C++' started by Frank Neuhaus, Aug 3, 2009.

  1. Hey,

    I'll get right to the issue (which is admittedly minor...):

    Made up example (does not make sense):

    #define SOMETHING \
    public: \
    ???* getInst() { return this; }

    class MyClass
    {
    SOMETHING
    };

    Just curious: Is there anything (unrelated to MyClass) I can put instead of
    ??? to make this work? Some boost tricks maybe that I haven't found so far?

    (I'm aware you can do it with SOMETHING(MyClass), or by deriving from a
    template class etc... I am interested in exactly this case)

    Thanks
    Frank
    Frank Neuhaus, Aug 3, 2009
    #1
    1. Advertising

  2. Frank Neuhaus wrote:
    > I'll get right to the issue (which is admittedly minor...):
    >
    > Made up example (does not make sense):
    >
    > #define SOMETHING \
    > public: \
    > ???* getInst() { return this; }
    >
    > class MyClass
    > {
    > SOMETHING
    > };
    >
    > Just curious: Is there anything (unrelated to MyClass) I can put instead
    > of ??? to make this work? Some boost tricks maybe that I haven't found
    > so far?
    >
    > (I'm aware you can do it with SOMETHING(MyClass), or by deriving from a
    > template class etc... I am interested in exactly this case)


    I don't think there exists any way. At least in all toolkits I've been
    exposed to, all those blocks usually have the class name spelled like
    you showed: SOMETHING(MyClass). You can probably use some additional
    trick to assert if the macro argument is not the name of the class
    itself, but the user of your macro would still have to supply it.

    Just my $0.02...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 3, 2009
    #2
    1. Advertising

  3. "Frank Neuhaus" <> writes:

    > Hey,
    >
    > I'll get right to the issue (which is admittedly minor...):
    >
    > Made up example (does not make sense):
    >
    > #define SOMETHING \
    > public: \
    > ???* getInst() { return this; }
    >
    > class MyClass
    > {
    > SOMETHING
    > };
    >
    > Just curious: Is there anything (unrelated to MyClass) I can put
    > instead of ??? to make this work? Some boost tricks maybe that I
    > haven't found so far?
    >
    > (I'm aware you can do it with SOMETHING(MyClass), or by deriving from
    > a template class etc... I am interested in exactly this case)


    Boost works in the compiler. #define works in the preprocessor, long
    before the compiler is even loaded in memory (at least conceptually).

    /* __VA_ARGS__ is available in g++ */
    #define CLASS(NAME,...) class CLASS __VA_ARGS__ { public: typedef CLASS Class; private:
    #define DEFAULT_METHODS public: Class* getInstance(){ return this; } private:
    #define ENDCLASS };


    CLASS(MyClass,:public MyRoot)
    DEFAULT_METHODS
    public:
    MyClass(){};
    virtual ~MyClass(){};
    //...
    ENDCLASS



    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Aug 3, 2009
    #3
  4. "Pascal J. Bourguignon" <> schrieb im Newsbeitrag
    news:...
    > "Frank Neuhaus" <> writes:
    > [...]
    >>
    >> (I'm aware you can do it with SOMETHING(MyClass), or by deriving from
    >> a template class etc... I am interested in exactly this case)

    >
    > Boost works in the compiler. #define works in the preprocessor, long
    > before the compiler is even loaded in memory (at least conceptually).


    I am aware of that of course. I just thought there might exist something
    like (i know this specific thing doesnt work because this isnt available
    outside of a member function...)

    BOOST_TYPEOF(this) getInst() { return this; }

    But well...

    > /* __VA_ARGS__ is available in g++ */
    > #define CLASS(NAME,...) class CLASS __VA_ARGS__ { public: typedef CLASS
    > Class; private:
    > #define DEFAULT_METHODS public: Class* getInstance(){ return this; }
    > private:
    > #define ENDCLASS };
    >
    >
    > CLASS(MyClass,:public MyRoot)
    > DEFAULT_METHODS
    > public:
    > MyClass(){};
    > virtual ~MyClass(){};
    > //...
    > ENDCLASS


    Yes I know those solutions. I was just wondering about this specific case.
    Thanks both of you - I guess what I intended just doesn't exist then :)

    Frank
    Frank Neuhaus, Aug 4, 2009
    #4
    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. Robert Hanson
    Replies:
    4
    Views:
    554
    Tu-Thach
    Jul 22, 2003
  2. Robert Hanson
    Replies:
    1
    Views:
    335
    Robert V. Hanson
    Jul 22, 2003
  3. E11
    Replies:
    1
    Views:
    4,686
    Thomas Weidenfeller
    Oct 12, 2005
  4. Nate Eldredge

    Initializing compound type containing opaque type

    Nate Eldredge, Oct 1, 2008, in forum: C Programming
    Replies:
    23
    Views:
    767
    David Thompson
    Oct 13, 2008
  5. scg_
    Replies:
    6
    Views:
    340
    Noah Roberts
    Feb 3, 2009
Loading...

Share This Page