Thread: stack and heap objects

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    stack and heap objects

    I want to simply create stack based objects but I have the following problem. I have a class which will contain a stack based static Queue. But I need to access it from my main.cpp source file, so I'm not sure how to declare it within my main.cpp file. If it's a stack based static queue, what do I write in my main.cpp? Also, what type of "Accessor" should I write for it in my class in order to get it. What's the return type of the "Accessor method? It shouldn't be a "*", should it? Do i return the "&" address of the stack based queue in order to access it in main? Then is that variable in main that references it a pointer? I'm a little confused..Also, as long as my main.cpp includes class A's header, can I call the accessor for the Queue inside main?

    Code:
    class A
    {
        public:
    
        static Queue myQueue; //on the stack
    
        Queue getMyQueue(); //accessor for it
    }
    
    Queue A::myQueue; //declare the stack variable
    
    Queue A::getMyQueue()
    {
        return &Queue; //in order to access it out of the class and insert into it
    }
    
    main.cpp (assuming all proper header inclusions)
    -----------
    
    Queue myQueue; //want to access it and insert into it
    
    myQueue = A::getMyQueue();
    
    myQueue.insert( blah );  //in order to keep in stack based?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I am confused by your terminology.

    If you want a static Queue for your class A (meaning only one Queue exists for all instances of A), and you want to access that from main, you could return a reference or pointer like this:
    Code:
    // return reference
    Queue& A::getMyQueue() { return Queue; }
    Code:
    // return pointer
    Queue* A::getMyQueue() { return &Queue; }
    I prefer the first, since the syntax is cleaner and it indicates that the return value will never be null. To use it, you could do either of the following:
    Code:
    A::getMyQueue().insert( blah );
    
    // or
    
    Queue& myQueue = A::getMyQueue();
    myQueue.insert( blah );
    Of course, in your example, myQueue is public so there really isn't a need for an accessor and you can just do this:
    Code:
    A::myQueue.insert( blah );
    Is that what you're looking for? It doesn't have anything to do with stack vs heap, though.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    ya, that makes sense, thanks.

    Another problem that arose for me is trying to return a formatted string of all a classes variables. I'm trying to use stringstring but am having no luck with anything longer than one variable. How can I properly format an output string, that contains all of a class's variables?

    Code:
    string classA::print()
    {
    
    stringstream in
    string out = " ";
    
    in << variableOne
    in << "   ";
    in << variableTwo;
    in << "   ";
    in << variableThree;
    
    in >> out;
    
    return out;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Note that "static" means that it's NOT stack-based, but rather "like a global".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> How can I properly format an output string, that contains all of a class's variables?
    Your code is pretty close, although it's missing some semi-colons. Did you try it?

    You don't actually need the out variable. Once you have streamed the information into the in variable, just return in.str(). The str() function of a stringstream returns the string that is in the stream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack vs heap memory allocation
    By gongchengshi in forum C Programming
    Replies: 9
    Last Post: 11-18-2007, 12:17 PM
  2. iterators heap or stack
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-22-2004, 03:40 PM
  3. Relocatable code and Symbol Table
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-10-2002, 11:05 AM
  4. What is a Stack, Heap and Queue ?
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 03-17-2002, 12:16 PM
  5. Stack and Heap
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-29-2002, 09:11 AM