The following code will not run under the SDK. It crashes with the following error:
[shell: child terminated due to signal 7: SIGSEGV: Invalid memory access]
Here is the code taken from 'Learn C++ in 21 Days'.
//Listing 6.4
#include iostream.h
class Cat {
public:
Cat(int initialAge);
~Cat();
int GetAge();
void SetAge(int age);
void Meow();
private:
int itsAge;
};
Cat::Cat(int initialAge)
{
itsAge = initialAge;
}
Cat::~Cat()
{
}
int Cat::GetAge()
{
return(itsAge);
}
void Cat::SetAge(int age)
{
itsAge = age;
}
void Cat::Meow()
{
cout << "Meow.\n";
}
int main()
{
Cat Frisky(5);
Frisky.Meow();
cout << "Frisky is a cat who is " << Frisky.GetAge() << " years old.\n";
Frisky.Meow();
Frisky.SetAge(7);
cout << "Now Frisky is " << Frisky.GetAge() << " years old.\n";
return(0);
}
The line from main() 'Cat Frisky(5);' is where the problem lies. It seems the initialization of 5 is never executed, or the whole constructor is never called.
I compiled the same program using g++, I get almost the same result in that the code will run, however the first use of the GetAge() method returns a huge number, decidely NOT 5.
I also compiled the code under SAS/C++ and Codewarrior for Windows. Both worked as expected. |