Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Variable declarable in function body, but not class member?

I want to create a C++ class with the following type:

  1. It can be declared inside of a function.
  2. It can be declared inside of a member function.
  3. It can not be declared as a class member.

The use of this: think "Root" objects for a GC.

Is this possible in C++? In particular, I'm using g++. Willing to switch to clang. Either templates or macro solution fine.

Thanks!

like image 667
anon Avatar asked Mar 23 '10 05:03

anon


1 Answers

You could do it with a macro, perhaps:

#define MY_TYPE \
    do { } while(0); \
    RealType

void foo() {
    MY_TYPE myvar;
    myvar.Whatever();
}

This would only compile inside a function (because of the "do ... while" bit - though you'd get a really weird error message). It seems like one of those "evil" uses of macros that you would want to avoid, however...

like image 124
Dean Harding Avatar answered Sep 22 '22 10:09

Dean Harding