Click here to Skip to main content
15,918,706 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Classes:
Chair;
BigChair; (no innherit)

Chair.h:
int _bigChairsLength;
BigChair** _bigChairs;

Chair.cpp:
C++
void Chair::AddBigchair(string name, string color) // It must get params and not BigChair
{
     BigChair** temp = _bigChairs;
     _bigChairsLength++;
     _bigChairs = new BigChair*[_bigChairsLength];

     for (int i=0; i<_bigChairsLength - 1; i++)
     {
         _bigChairs[i] = temp[i];
     }

     delete [] temp;

     BigChair newBigChair(name, color);
     _bigChairs[_bigChiarsLength - 1] = &newBigChair;
              // (pointer to function variable??? how to add?
}
Posted
Updated 22-Nov-13 13:03pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Nov-13 19:22pm    
Not clear. This is just a code dump. What do you want to achieve?
—SA
Member 7966831 22-Nov-13 19:33pm    
Was desctibed in the comment
Andreas Gieriet 24-Nov-13 2:56am    
Since you did not care to ask a decent question (you seem not to care to ask with any full sentences - one has to dig deep into the details), your question was voted down, and therefore, most likely no one would take the trouble to read and answer your question. That's how it works.
Make it easy to the readers to answer your questions: Write as first sentences what the situation is, what you did try out so far, and where you struggle. Then only comes the code.
Cheers
Andi
Member 7966831 24-Nov-13 18:53pm    
Sure.

1 solution

Your new array gets a pointer of a local variable (an element from the stack instead of from the heap). That variable gets destroyed and the pointer points to garbage. In fact, it points to some memory that will eventually hold other data and you will severly corrupt the program memory - unpredictable crash sooner or later...
Fix:
C++
_bigChairs[_bigChiarsLength - 1] = new BigChair(name, color);

Is this a homweork assignment or an interview question? This is so basic.
Cheers
Andi

PS: use a suitable STL container (e.g. a vector) instead of a pointer of pointer. E.g. std::vector<BigChair> (or std::vector<BigChair*>, depends on you usage). The whole function would collaps to mvVector.push_back(BigChair(name, color));.
 
Share this answer
 
v3
Comments
Member 7966831 22-Nov-13 19:32pm    
Thanks for your reply.
But how is it that you add an object instead of pointer to the array?
It is defined as BigChair**
Andreas Gieriet 24-Nov-13 3:01am    
You obviously implement a container. Why re-inventing the wheel? Use the standard library containers (e.g. a vector) instead. And as I said, dependeing on your application, you might add an object instead of a pointer to an object. You say you have no polymorphism, so an object would do.
Cheers
Andi
Member 7966831 24-Nov-13 18:51pm    
The issue was I didn't know 'new' operator return a pointer and not an object. Now it makes sense, thanks.
Andreas Gieriet 25-Nov-13 4:09am    
You really have to be aware of the difference between Java/C# and C/C++ when it comes to memory management, especially the difference of new between Java/C# and C++.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900