0

i have two classes (non-static) A & (static) B. I am trying to store Object A into B so that i can use its functions in funcB(). However i do know that static classes cannot store non-static variables & functions. is there anyway to get pass this rather than converting Class A into a static class?

class A
{
   public:
          A();
          void funcA();
   private:
          int A;
};

class B
{
   public:
         B(A *objA);
         static void funcB();
   private:
         A *objA;
};

edit: I stated static class to make it easier to explain. i did not know the correct term. So the question is actually: How do i use a non-static member from a static function?

9
  • Why not pass a pointer or reference of A to funcB? Commented Jan 29, 2013 at 3:57
  • 4
    "Static classes" do not exist and your question makes no sense. Please explain why you think you need this. Commented Jan 29, 2013 at 3:58
  • There's no such thing as a "static class". Maybe you mean a singleton class who, conceptually, only have static members. Also, instances of your "static class" can have non-static members. Only that static methods cannot use those non-static members for they need an instance for them to be used. Commented Jan 29, 2013 at 3:58
  • Sorry i said static classes as it was easier to explain. i do know that they do not exist in c++. but static functions do. However, when declaring static functions, i am unable to use the *objA Pointer. i need this because, the class A, links to many other classes and has stored many other information. Class B on the other hand, has to be static due to some windows API threading condition. Commented Jan 29, 2013 at 3:59
  • @MarkGarcia how do you declare an instance such that the static methods can use the non-static members? Commented Jan 29, 2013 at 4:01

2 Answers 2

1

You can not access anything that is specific to an instance of a class from a static function by itself. A static function has no "this" pointer (the compiler passes a pointer to the instance of the object calling the function for non-static functions). You can get around this by passing a pointer to the object you want to modify, but then why are you using a static function? My advice would be to avoid what it seems like you are trying to do, but I provided 2 ways to do it below (as a good learning example).

See below for an example of using

    #include <iostream>

using namespace std;

class A
{
   public:
    A(int value) : a(value){}
          void funcA();
   public:
          int a;
};

class B
{
   public:
         B()
         {
             objA = new A(12);
         }

         void funcB2()
         {
             B::funcB(*objA);
         }

         static void funcB(A const & value)
         {
            cout << "Hello World! " << value.a << endl;
         }

   private:
         A *objA;
};

int main()
{
    A a(10);
    B::funcB(a);

    B b;
    b.funcB2();
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you just should not make design like this. A static function is initialized when the non-static members are not ready. You said "the class A, links to many other classes and has stored many other information. Class B on the other hand, has to be static due to some windows API threading condition." . In this case, can you change your design and turn A into static class?

1 Comment

if i turn A into a static class, then the many other classes that includes will be affected. this will be a problem. i am actually using windows FileSystemWatcher which requires static methods. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.