Solved

Is This Valid C++?

Posted on 1998-06-29
1
194 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 50 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now