Click here to Skip to main content
15,891,184 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: what is wrong with this code? Pin
PIEBALDconsult22-Jan-11 10:15
mvePIEBALDconsult22-Jan-11 10:15 
AnswerRe: what is wrong with this code? Pin
CPallini22-Jan-11 10:39
mveCPallini22-Jan-11 10:39 
AnswerRe: what is wrong with this code? Pin
T210222-Jan-11 13:08
T210222-Jan-11 13:08 
AnswerRe: what is wrong with this code? [modified] Pin
Richard MacCutchan22-Jan-11 21:35
mveRichard MacCutchan22-Jan-11 21:35 
AnswerRe: what is wrong with this code? Pin
Aescleal23-Jan-11 6:34
Aescleal23-Jan-11 6:34 
GeneralRe: what is wrong with this code? Pin
Adam Roderick J23-Jan-11 19:04
Adam Roderick J23-Jan-11 19:04 
GeneralRe: what is wrong with this code? Pin
PIEBALDconsult24-Jan-11 2:02
mvePIEBALDconsult24-Jan-11 2:02 
AnswerRe: what is wrong with this code? [modified] Pin
Stefan_Lang30-Jan-11 23:54
Stefan_Lang30-Jan-11 23:54 
Building on some of the already posed answers:

As mentioned before, you need a destructor to make sure pi is released.

Moreover, if you provide no Copy constructor or assignment operator, the compiler will create these for you. Both will simply copy pi over without checking, und thus also produce a memory leak. This means you need to define both a copy constructor and an assignment operator yourself, to prevent memory leaks from inadvertent copying!

Another, minor, issue is that you didn't think to initialize the integer pi is pointing to.

I will ignore the apparent uselessness of the class in it's current state, assuming that it's going to be expanded later. Going with the above, I would rewrite this to sth like that:
class A {
   int* pi;
public:
   int* getpi() {
      if (0 == pi)
         pi = new int();
         if (pi != 0)
            *pi = 0;
      return pi;
   }
   A() : pi(0) {}
   A(const A& other) : pi(0) {
      *getpi() = *other.pi; /* deep copy */
   }
   ~A() { delete pi; }
   A& operator=(const A& other) {
      *getpi() = *other.pi; /* deep copy */
      return *this;
   }
}

Note that you could also implement a shallow copy by just redirecting pi to wherever the other object's pi is pointing, but if you do that, you need some mechanism such as reference counting to prevent deleting an int object while oter instances are still pointing to it!

Note also that I only programmed the happy case with respect to allocation: if getpi() ever fails to allocate pi (and thus returns 0), then the call to the copy constructor or assignment operator will throw a NULL pointer exception. You might want to catch that, especially in the constructor.

[edit]Made getpi() public to resolve the inaccessibility issue, and added initializion of *pi[/edit]

modified on Monday, January 31, 2011 6:02 AM

QuestionRetaining whitespace within macro replacement text Pin
PIEBALDconsult22-Jan-11 4:17
mvePIEBALDconsult22-Jan-11 4:17 
AnswerRe: Retaining whitespace within macro replacement text Pin
Andrew Brock22-Jan-11 4:38
Andrew Brock22-Jan-11 4:38 
GeneralRe: Retaining whitespace within macro replacement text Pin
PIEBALDconsult22-Jan-11 9:00
mvePIEBALDconsult22-Jan-11 9:00 
GeneralRe: Retaining whitespace within macro replacement text Pin
jschell22-Jan-11 12:01
jschell22-Jan-11 12:01 
GeneralRe: Retaining whitespace within macro replacement text Pin
PIEBALDconsult22-Jan-11 18:06
mvePIEBALDconsult22-Jan-11 18:06 
GeneralRe: Retaining whitespace within macro replacement text Pin
jschell23-Jan-11 10:40
jschell23-Jan-11 10:40 
QuestionUnknown problem with non-english languages [modified] Pin
rahul.kulshreshtha21-Jan-11 23:17
rahul.kulshreshtha21-Jan-11 23:17 
AnswerRe: Unknown problem with non-english languages Pin
Andrew Brock21-Jan-11 23:39
Andrew Brock21-Jan-11 23:39 
GeneralRe: Unknown problem with non-english languages Pin
rahul.kulshreshtha21-Jan-11 23:53
rahul.kulshreshtha21-Jan-11 23:53 
GeneralRe: Unknown problem with non-english languages Pin
Andrew Brock22-Jan-11 0:38
Andrew Brock22-Jan-11 0:38 
GeneralRe: Unknown problem with non-english languages Pin
rahul.kulshreshtha22-Jan-11 0:56
rahul.kulshreshtha22-Jan-11 0:56 
GeneralRe: Unknown problem with non-english languages Pin
Andrew Brock22-Jan-11 1:21
Andrew Brock22-Jan-11 1:21 
GeneralRe: Unknown problem with non-english languages Pin
rahul.kulshreshtha22-Jan-11 2:13
rahul.kulshreshtha22-Jan-11 2:13 
GeneralRe: Unknown problem with non-english languages Pin
Andrew Brock22-Jan-11 2:39
Andrew Brock22-Jan-11 2:39 
GeneralRe: Unknown problem with non-english languages Pin
Richard MacCutchan22-Jan-11 2:11
mveRichard MacCutchan22-Jan-11 2:11 
GeneralRe: Unknown problem with non-english languages Pin
Andrew Brock22-Jan-11 2:27
Andrew Brock22-Jan-11 2:27 
GeneralRe: Unknown problem with non-english languages Pin
Richard MacCutchan22-Jan-11 6:30
mveRichard MacCutchan22-Jan-11 6:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.