Click here to Skip to main content
15,905,071 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Question about memory allocation of data members of a class. Pin
Tim Smith17-May-03 1:41
Tim Smith17-May-03 1:41 
GeneralRe: Question about memory allocation of data members of a class. Pin
George217-May-03 3:08
George217-May-03 3:08 
Generalremoving a control and disable message during run-time Pin
yccheok16-May-03 21:22
yccheok16-May-03 21:22 
GeneralRe: removing a control and disable message during run-time Pin
valikac17-May-03 6:04
valikac17-May-03 6:04 
GeneralVisual C++ 6.0 - Rapid Application Development Pin
Anonymous16-May-03 20:46
Anonymous16-May-03 20:46 
GeneralGlobal static function Pin
Shah Shehpori16-May-03 18:53
sussShah Shehpori16-May-03 18:53 
GeneralRe: Global static function Pin
Neville Franks17-May-03 2:11
Neville Franks17-May-03 2:11 
Generalclass operators strange results Pin
Rui Dias Lopes16-May-03 16:52
Rui Dias Lopes16-May-03 16:52 
Hi there, i'm having trouble with some c++ code, regarding classes and operators. Given the following code:
class TestInt {
private:
  int _value;

public:
  TestInt() : _value(0) {}

  TestInt(int newValue) {
    TestInt();
    _value = newValue;
  }

  ~TestInt() {}

  operator int () {
    int aux;

    aux = _value;

    return aux;
  }

  void operator = (int newValue) {
    _value = newValue;
  }
};

and
int main() {
  TestInt t = 0;
  TestInt x = 0;
  TestInt y = 0;

  int i = 0;

  t = 50;
  x = 100;
  y = 300;

  printf("i is %d, t is %d, x is %d, y is %d\n", i, t, x, y);

  i = t;
  t = 60;
  t = x;

  printf("i is %d, t is %d, x is %d, y is %d\n", i, t, x, y);

  return 0;
}

I think it's supposed to output
i is 0, t is 50, x is 100, y is 300
i is 50, t is 100, x is 100, y is 300

but instead, it outputs
i is 0, t is 50, x is 2081702084, y is 88
i is 50, t is 100, x is 2081702084, y is 88

Can someone point out what's wrong with the posted code? oh, by the way, yes... operator int() really does need the int aux local variable because this code is only a simple example and yet, does not work. These weird results look like somethin's happening in the stack that shouldn't be.

thanks in advance for the help,

Rui

is this a sig?
GeneralRe: class operators strange results Pin
Andrew Walker16-May-03 17:10
Andrew Walker16-May-03 17:10 
GeneralRe: class operators strange results Pin
Rui Dias Lopes16-May-03 22:29
Rui Dias Lopes16-May-03 22:29 
GeneralRe: class operators strange results Pin
Andrew Walker16-May-03 23:13
Andrew Walker16-May-03 23:13 
GeneralRe: class operators strange results Pin
Rui Dias Lopes16-May-03 23:18
Rui Dias Lopes16-May-03 23:18 
GeneralRe: class operators strange results Pin
Andrew Walker16-May-03 23:33
Andrew Walker16-May-03 23:33 
GeneralNoob help! Pin
weezer16-May-03 14:42
weezer16-May-03 14:42 
GeneralRe: Noob help! Pin
valikac16-May-03 16:17
valikac16-May-03 16:17 
GeneralRe: Noob help! Pin
Anonymous17-May-03 10:21
Anonymous17-May-03 10:21 
GeneralRe: Noob help! Pin
Nitron16-May-03 20:00
Nitron16-May-03 20:00 
GeneralPassing a pointer to member function in Visual C++ 6.0 Pin
Gagnon Claude16-May-03 12:56
Gagnon Claude16-May-03 12:56 
GeneralRe: Passing a pointer to member function in Visual C++ 6.0 Pin
Michael Dunn16-May-03 13:46
sitebuilderMichael Dunn16-May-03 13:46 
Generalenter an integer data Pin
Member 430384616-May-03 11:29
Member 430384616-May-03 11:29 
QuestionHow to load a gif Pin
amcan2716-May-03 11:05
amcan2716-May-03 11:05 
AnswerRe: How to load a gif Pin
Bartosz Bien16-May-03 14:23
Bartosz Bien16-May-03 14:23 
AnswerRe: How to load a gif Pin
SoloVision16-May-03 20:00
SoloVision16-May-03 20:00 
GeneralGetting Windows with a taskbar button Pin
Waleed Eissa16-May-03 10:42
Waleed Eissa16-May-03 10:42 
GeneralRe: Getting Windows with a taskbar button Pin
Baris Kurtlutepe16-May-03 13:59
Baris Kurtlutepe16-May-03 13:59 

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.