Type of containing class

F

Frank Neuhaus

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
 
V

Victor Bazarov

Frank said:
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
 
P

Pascal J. Bourguignon

Frank Neuhaus said:
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
 
F

Frank Neuhaus

Pascal J. Bourguignon said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top