Solved

Is This Valid C++?

Posted on 1998-06-29
1
199 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

937 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

3 Experts available now in Live!

Get 1:1 Help Now