Solved

Is This Valid C++?

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

751 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