About classes.

Hello,

i'm wondering if there's any difference between pointer definition:

Ione* pointer = new two;

and

two* pointer = new two;


Ione is a base class for two
example:
1
2
3
4
5
6
7
8
9
10
class Ione
{
    public:
    ~~~
 
};

class two : public Ione
{
};


Thanks for reply,
Magson.
There is a difference. Your "pointer" knows that the memory it points to should have the properties of class Ione, while your "pointer" knows that the memory it points to should have the properties of class two. (Your example uses ambiguous identifiers, which makes referencing difficult, doesn't it?)


In recommended design the base class defines an interface and the derived classes merely adjust the implementation. Therefore, all objects can be used via base class pointers; the user (pointer) does not need to know the true type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Base {
public:
  Base();
  void foo () { vfoo(); }
private:
  virtual void vfoo();
};

class Derived : public Base {
public:
  Derived() : Base() {}
  void bizarre(); // questionable deviation from Base's interface
private:
  virtual void vfoo();
};

...
Derived * p1 = new Derived;
Base * p2 = p1;

p1->foo(); // ok, Derived::vfoo() is called
p2->foo(); // ok, Derived::vfoo() is called

p1->bizarre(); // ok
p2->bizarre(); // error: there is no Base::bizarre() 
Topic archived. No new replies allowed.