Inheritance & Polymorphism: Why dangerous??

Hi Everyone! I'm new here and am excited to learn C++ from everyone! This is my first post. I apologise if this question may have been asked before.

I have 2 questions.

1. I can't tell why that one line of code is dangerous.
Attempt: The const should not be used so that m_szName can be different.

2. On line 3, I have no idea what is Cperson and &oPerson for. And why use const?
Attempt: Two variables called CPerson and the address operator of oPerson must be constant.

Help is very much appreciated :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class CPerson {
public:
   CPerson(const char *szName=NULL);
   CPerson(const CPerson &oPerson); // Q2 What does this line do?? 
   virtual ~CPerson();

   CPerson operator=(const CPerson &oPerson);

   void SetName(const char *szName);
   const char *GetName() const {return m_szName;};
      // Q1 this is actually very dangerous ... why?

   virtual void GetDescriptionText(char *szDesc, int nMaxLength);
protected:
   char *m_szName;
};

Last edited on
2)
You're confusing the address of operator with the reference operator. (They're both the & symbol).

const CPerson& oPerson; makes a const reference to another CPerson object.

Essentially CPerson(const CPerson &oPerson); is what's called a "copy constructor". It is the constructor that is called whenever on object is being copied to another. The 'const' is there because the object being copied shouldn't be changed, and is therefore constant.

For example:
1
2
CPerson bob("Bob");  // makes a person named bob
CPerson anotherbob = bob;  // copies bob to anotherbob .. copy constructor called 



1) It's not really that dangerous. std::string does something similar with the c_str() function.

Reasons it could be considered dangerous:

- the returned pointer will become invalid if the CPerson object is destroyed, or if the object moves the buffer elsewhere
- char arrays in general are dangerous because they require you be responsible to prevent buffer overflows and memory management.


0) Your = operator should return a reference, not a copy. Line 7 should be: CPerson& operator = (const CPerson& operson); note the additional & symbol.
thanks Disch! Just to clarify further.

2) So basically, this copy constructor makes sure that the derived classes get bob without "Bob" changing in both base & derived class?

1) Destroyed? So for example, line 10 is deleted. And when the main cpp file calls *GetName(), the program crashes?

0) THANKS! I didn't see that. I guess my lecturer is testing me. I will write about that mistake.
2) Err.... derived classes have nothing to do with it.

Whenever an object is created, a constructor is called. Which constructor is called depends on the parameters passed:

1
2
CPerson bob("Bob");  // calls the constructor that takes a const char* as a param
CPerson bob2(bob);  // calls the constructor that takes a const CPerson& as a param 


The "copy constructor" is called that because it's called whenever you construct a copy of an existing object... such is the case with bob2 above.


1) No destroyed as in the object is destroyed. For example:

1
2
3
4
5
6
7
8
9
10
const char* ptr;

{
  CPerson bob("Bob");
  ptr = bob.GetName();
  cout << ptr;  //  this is OK because 'bob' still exists
}  // here, 'bob' goes out of scope and is destroyed.

// here, 'bob' no longer exists, so 'ptr' is not a bad pointer
cout << ptr;  // BAD, ptr points to something that no longer exists 
Topic archived. No new replies allowed.