Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Is This Valid C++?

Posted on 1998-06-29
1
Medium Priority
?
241 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
1 Comment
 
LVL 1

Accepted Solution

by:
cmw earned 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

972 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