-
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?
-
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.
-
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;
}
-
Note that "static" means that it's NOT stack-based, but rather "like a global".
--
Mats
-
>> 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.