sizeof(non_static_member_variable) in a static function

N

nicolas.gendron

Is there a way to do this?

I don't want to use sizeof(non_static_member_variable_type) because if
the type of my member variable changes, the sizeof operator won't be
informe.

Any solution?

Thanks

Nic
 
P

Puppet_Sock

Is there a way to do this?

I don't want to use sizeof(non_static_member_variable_type) because if
the type of my member variable changes, the sizeof operator won't be
informe.

Any solution?

I suspect you may want to re-factor something there. If you've
got a static function that needs to know the size of a non-static
data member, you have already passed the line you seem to
be worried about. You've got a static func that needs to know
implementation details of non-static parts of a class. What if
a future implementation does not have that data member at all?

What about taking the task from this static function and putting
it in a non-static function? If the static function needs to know
stuff about the non-static members, it needs to know these
details about a particular instance of the class, and to call
that instance for the results.

If that simply won't fly, then what about something like so?
Create an instance of the class in the static func, and get
the details you need from that instance.

class A
{
public:
static int myFunc(){A a; return sizeof(a.myVar);}
private:
int myVar;
};

Kind of klunky, I agree. And if your class is large it could produce
unacceptable overhead to make this extra instance.
Socks
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top