Thread: Instantiation of objects in a class

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

    Instantiation of objects in a class

    Hi,

    I want to instantiate objects in a class using 3 scenarios:
    -Stack
    -Heap
    -static
    with constructors and destructors.Now I have 2 questions:
    1. How I can realize the scenario with static?
    2.How I realize that the call of the constructor is showing to which scenario it belongs as well.

    Thank you.
    Last edited by Joe1903; 11-14-2016 at 05:30 AM.

  2. #2
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Has no one an idea?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    -Stack => a local variable
    -Heap => you call new or new[]
    -static => you declare it outside of a function
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    -Stack => a local variable
    -Heap => you call new or new[]
    -static => you declare it outside of a function
    I have that:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    class X
    {
            public:
                   X();
    
                   ~X();
            static void Y (void);
            static string my_str;
    };
    X::X(void)
    {
            cout << "Call constructor" << endl;
    }
    
    X::~X(void)
    {
            cout << "Call destructor" << endl;
    }
    Y::init(void)
    {
            my_str = "Call static constructor";
    }
    int main( )
    {
            X x;
            X *xarray = new X[5];
            delete [] xarray;
            Y::init();
            return (0);
    }
    But this is not working. Where are my mistake(s)?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You didn't declare what Y is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    You didn't declare what Y is.
    Hmm ok. But is the rest correct?

  7. #7
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    You didn't declare what Y is.
    Is that one correct?I mean it is compiling and executing,I am also sure with heap and stack,but not sure with static..

    Code:
    #include <iostream>
    using namespace std;
    class Line
    {
            public:
                    void setLength( double len );
                    double getLength( void );
                    Line();
                    ~Line();
            private:
                    double length;
                    static bool IsInit;
                    static bool Init();
    };
    
    Line::Line(void)
    {
            cout << "Call constructor" << endl;
    }
    Line::~Line(void)
    {
            cout << "Call destructor" << endl;
    }
    void Line::setLength( double len )
    {
            length = len;
    }
    double Line::getLength( void )
    {
            return length;
    }
    bool Line::IsInit(Init());
    bool Line::Init()
    {
            cout << "Call static constructor" << endl;
            return 0;
    }
    int main( )
    {
            Line line;
            Line *array_on_heap = new Line[5];
            delete [] array_on_heap;
            line.setLength(6.0);
            cout << "Length of line : " << line.getLength() <<endl;
            return 0;
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you could put
    cout << "Start of main()" << endl;
    as the very first line of main.

    The static things should be printed before this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    Well you could put
    cout << "Start of main()" << endl;
    as the very first line of main.

    The static things should be printed before this.
    Is it possible to "separate" the constructor and destructor calls for heap and stack? Like static.

  10. #10
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Joe1903 View Post
    Is it possible to "separate" the constructor and destructor calls for heap and stack? Like static.
    I' ve done that:

    Code:
    #include <iostream>
    using namespace std;
    class Line
    {
            public:
                    void setLength( double len );
                    double getLength( void );
                    Line();
                    ~Line();
            private:
                    double length;
                    static bool IsInit;
                    static bool Init();
    };
    
    Line::Line(void)
    {
            cout << "Call constructor" << endl;
    }
    Line::~Line(void)
    {
            cout << "Call destructor" << endl;
    }
    void Line::setLength( double len )
    {
            length = len;
    }
    double Line::getLength( void )
    {
            return length;
    }
    bool Line::IsInit(Init());
    bool Line::Init()
    {
            cout << "Call static constructor" << endl;
            return true;
    }
    int main( )
    {
            cout << "Start of main()" << endl;
            cout << "Start of stack allocation" << endl;
            Line line;
            line.setLength(6.0);
            cout << "Length of line: " << line.getLength() << endl;
            cout << "Start of heap allocation" << endl;
            Line *array_on_heap = new Line[5];
            cout << "Free heap starting" << endl;
            delete [] array_on_heap;
            cout << "Free heap done" << endl;
            cout << "Free stack" << endl;
            return (0);
    }
    Does it look good?And how can I realize a static destructor?
    Last edited by Joe1903; 11-17-2016 at 08:11 AM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I was thinking of something like this
    Code:
    #include <iostream>
    using namespace std;
    class Blob
    {
    public:
      Blob();
      ~Blob();
    };
    Blob::Blob(void)
    {
            cout << "Blob: Call constructor for instance at " << (void*)this << endl;
    }
    Blob::~Blob(void)
    {
            cout << "Blob: Call destructor for instance at " << (void*)this << endl;
    }
    
    
    class Line
    {
      static Blob blobby;
    public:
      Line();
      ~Line();
    };
    Blob Line::blobby;
    
    Line::Line(void)
    {
            cout << "Line: Call constructor for instance at " << (void*)this << endl;
    }
    Line::~Line(void)
    {
            cout << "Line: Call destructor for instance at " << (void*)this << endl;
    }
    
    Line global;
    
    int main ( ) {
      cout << "Start of main" << endl;
      Line line;
    
      cout << "Start of heap allocation" << endl;
      Line *array_on_heap = new Line[5];
      cout << "Free heap starting" << endl;
      delete [] array_on_heap;
      cout << "Free heap done" << endl;
    
      cout << "End of main" << endl;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global instantiation
    By kmdv in forum C++ Programming
    Replies: 0
    Last Post: 03-09-2011, 01:45 PM
  2. Template Instantiation
    By Tonto in forum C++ Programming
    Replies: 25
    Last Post: 02-17-2008, 09:01 AM
  3. Instantiation question
    By patricio2626 in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2007, 03:16 PM
  4. Template instantiation
    By ygfperson in forum C++ Programming
    Replies: 3
    Last Post: 06-12-2003, 03:04 PM
  5. Class Instantiation
    By JerryD in forum C++ Programming
    Replies: 3
    Last Post: 05-26-2002, 04:49 PM

Tags for this Thread