Solved

Is This Valid C++?

Posted on 1998-06-29
1
231 Views
Last Modified: 2010-04-01
Environment Visual C++ 5.0.

This compiles and appears to run OK but I was wondering whether it is valid (or good) C++. And if not is there a better way to do it?

class COther
{

protected:
      int x;
      int y;

public:
      COther();
      ~COther();
      virtual void set_values(int px,int py)
      {
            x = px;
            y = py;
      }
};

class A
{

protected:
      COther* other_class;

public:
      virtual void do_work()
      {
            other_class = new COther;
            other_class->set_values(2,4);
      }
};

class CNewOther : public COther
{
      int z;

public:
      CNewOther();
      ~CNewOther();
      virtual void set_values(int px,int py)
      {
            y = px;
            x = py;
            z = y*x;
      }
};

class B : public A
{
      virtual void do_work()
      {
            other_class = new CNewOther;
            other_class->set_values(2,4);
      }
};

This is a simple example of the actual code that has been written. It just struck me that this might potentially cause problems because the declared pointer is not the same as the instantiated type for the other_class object.

Any comments?
0
Comment
Question by:sdj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
cmw earned 50 total points
ID: 1166775
Yes, this is valid. It works because you declare the set_values method virtual. So code is inserted to find out what the pointer points to, and to call the correct method. Leave out the virtual, and the compiler will assume that type of the pointer is allways correct, and try to call COther::set_values(). The virtual needs only to be specified at the root level, but I prefer to mention it through the hierarchy.

As to the question if it can be done differently, well I suppose you think of all sorts of ways of doing this, but it really depends on what you are trying to achieve. In general, Objects of classes should encapsulate the behaviour for which they are responsible. You do not mention what your objective is, so this isn't as straightforward.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question