By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,301 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,301 IT Pros & Developers. It's quick & easy.

sizeof(non_static_member_variable) in a static function

P: n/a
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

Mar 30 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ni*************@gmail.com wrote:
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

Mar 30 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.