Thread: Static members in C++

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    84

    Static members in C++

    Hi,

    I want to do this exercise

    Define two classes, each with a static member,
    so that the construction of each static member involves a reference to the other.
    Where might such constructs appear in real code?
    How can these classes be modified to eliminate the order dependence in the constructors?

    Is my code correct?

    Code:
    #include <iostream>
    using namespace std;
     
    class StepOne
    {
         int num;
     public:
         StepOne() 
     { 
      cout << "StepOne's constructor called " << endl;  
     }
    };
     
    class StepTwo
    {
         static StepOne first;
     public:
         StepTwo() 
     { 
      cout << "StepTwo's constructor called " << endl; 
     }
         static StepOne getStepOne() 
     { 
      return (first); 
     }
    };
     
    StepOne StepTwo::first;
     
    int main()
    {
         StepTwo st1, st2, st3;
         StepOne first = st1.getStepOne();
     
         return (0);
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    No, you only have one class with a static member, the assignment calls for two.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by King Mir View Post
    No, you only have one class with a static member, the assignment calls for two.
    What about this? Where is the failure here?
    Code:
    #include <iostream>
    using namespace std;
    class StepOne
    {
     static StepTwo value_st;
            public:
            StepOne()
            {
                    cout << "StepOne's constructor called " << endl;
            }
     static StepTwo getStepTwo()
     {
      return (value_st);
     }
    };
    
    class StepTwo
    {
            static StepOne value_so;
            public:
            StepTwo()
            {
                    cout << "StepTwo's constructor called " << endl;
            }
            static StepOne getStepOne()
            {
                    return (value_so);
            }
    };
    StepOne StepTwo::value_so;
    StepTwo StepOne::value_st;
    int main()
    {
            StepTwo st1;
     StepOne st2;
            StepOne value_so = st1.getStepOne();
     StepTwo value_st = st2.getStepTwo();
            return (0);
    }

  4. #4
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Joe1903 View Post
    What about this? Where is the failure here?
    Code:
    #include <iostream>
    using namespace std;
    class StepOne
    {
     static StepTwo value_st;
            public:
            StepOne()
            {
                    cout << "StepOne's constructor called " << endl;
            }
     static StepTwo getStepTwo()
     {
      return (value_st);
     }
    };
    
    class StepTwo
    {
            static StepOne value_so;
            public:
            StepTwo()
            {
                    cout << "StepTwo's constructor called " << endl;
            }
            static StepOne getStepOne()
            {
                    return (value_so);
            }
    };
    StepOne StepTwo::value_so;
    StepTwo StepOne::value_st;
    int main()
    {
            StepTwo st1;
     StepOne st2;
            StepOne value_so = st1.getStepOne();
     StepTwo value_st = st2.getStepTwo();
            return (0);
    }
    First I had this one,but my collegue said it is not complete.What is missing here to fulfill the assignment?

    Code:
    #include <iostream>
    using namespace std;
    
    struct MemberA
    {
            public:
            int x;
            MemberA();
    };
    
    struct MemberB
    {
            int x;
            public:
            MemberB();
    };
    
    class ClassA
    {
            public:
            static MemberA x;
    };
    
    class ClassB
    {
            public:
            static MemberB x;
    };
    
    MemberA::MemberA()
    {
            x = ClassB::x.x;
            cout << "Aufruf MemberA Konstruktor" << endl;
    }
    
    MemberB::MemberB()
    {
            x = ClassA::x.x;
            cout << "Aufruf MemberB Konstruktor" << endl;
    }
    
    MemberA ClassA::x;
    MemberB ClassB::x;
    int main()
    {
    }

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Joe1903 View Post
    First I had this one,but my collegue said it is not complete.What is missing here to fulfill the assignment?

    Code:
    #include <iostream>
    using namespace std;
    
    struct MemberA
    {
            public:
            int x;
            MemberA();
    };
    
    struct MemberB
    {
            int x;
            public:
            MemberB();
    };
    
    class ClassA
    {
            public:
            static MemberA x;
    };
    
    class ClassB
    {
            public:
            static MemberB x;
    };
    
    MemberA::MemberA()
    {
            x = ClassB::x.x;
            cout << "Aufruf MemberA Konstruktor" << endl;
    }
    
    MemberB::MemberB()
    {
            x = ClassA::x.x;
            cout << "Aufruf MemberB Konstruktor" << endl;
    }
    
    MemberA ClassA::x;
    MemberB ClassB::x;
    int main()
    {
    }
    Can someone help me please?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-17-2011, 11:22 PM
  2. Static members in class
    By prongs_386 in forum C++ Programming
    Replies: 11
    Last Post: 01-03-2007, 06:36 AM
  3. static members construction
    By glUser3f in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2003, 09:58 PM
  4. Static Members...
    By BigDaddyDrew in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2003, 08:46 PM
  5. static and non-static data members
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2002, 10:06 AM

Tags for this Thread