473,748 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

creating methods for objects

Hi,

is it possible to override methods for one specific object, like you can
do in Ruby? Something like:

--8<--

class A {
public:
void method();
}

void A::method() {
// do something...
}

A x = new A();

void x::method() {
// do something else...
}

--8<--

Or anything close to that?

Thanks,

Andre'
Jul 22 '05 #1
18 1752
On Fri, 18 Jun 2004 16:31:01 -0300 in comp.lang.c++, André
<an**********@s yspoint.com.br> wrote,
is it possible to override methods for one specific object, like you can
do in Ruby?


No. You might declare a derived class just for that one object.
You might use a pointer to a function, or some similar low-level
hackery.

Jul 22 '05 #2
André wrote:
is it possible to override methods for one specific object, like you can
do in Ruby? Something like:

--8<--

class A {
public:
void method();
}

void A::method() {
// do something...
}

A x = new A();

void x::method() {
// do something else...
}

--8<--

Or anything close to that?


No.

C++ was invented to fit the C compiler and linker model, with minimal
changes to legacy compilers. Each translation unit must contain the same
definition of each method, just to simplify the linker's requirements.

Investigate function pointers and method pointers, to let 'x' point to a
method different from its class's method.

Why can't you just use Ruby if you need it? C++ is for big, low-level
systems; not for the average code written today.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #3

Untested code, but you'll get the jist.
class BarnyardAnimal
{
private:

int something;

static int EatDefault(Barn yardAnimal& me, int food)
{
me.something = 7 + food; //Accessing member variable
}

public:

int (*pEat)(Barnyar dAnimal&,int);

int Eat(int food)
{
pEat(*this,food );
}

BarnyardAnimal( void) : Eat(EatDefault)
{

}

};
int main(void)
{
BarnyardAnimal cow;

cow.Eat(5); //Default function

cow.Eat = ReturnFunctionP ointer();

cow.Eat(7); //Calls other function
}
-JKop
Jul 22 '05 #4
JKop posted:

BarnyardAnimal( void) : Eat(EatDefault)
{

}
TYPO TYPO TYPO

BarnyardAnimal( void) : pEat(EatDefault )
{

}


int main(void)
{
BarnyardAnimal cow;

cow.Eat(5); //Default function

cow.Eat = ReturnFunctionP ointer();

TYPO TYPO TYPO

cow.pEat = ReturnFunctionP ointer();


cow.Eat(7); //Calls other function
}

-JKop
Jul 22 '05 #5
Ignore my last 2 posts. I'll rewrite the whole lot here without typos and
errors. The following compiles:
class BarnyardAnimal
{
private:

int something;

static int EatDefault(Barn yardAnimal& me, int food)
{
me.something = 7 + food; //Accessing member variable
}

friend int EatOther(Barnya rdAnimal&, int);

public:

int (*pEat)(Barnyar dAnimal&,int);

int Eat(int food)
{
pEat(*this,food );
}

BarnyardAnimal( void) : pEat(EatDefault )
{

}

};

int EatOther(Barnya rdAnimal& me, int food)
{
;
}

int main(void)
{
BarnyardAnimal cow;

cow.Eat(5); //Default function

cow.pEat = EatOther;

cow.Eat(7); //Calls other function
}

-JKop
Jul 22 '05 #6
Phlip wrote:
C++ is for big, low-level
systems; not for the average code written today.

What's that suppose to mean? I use C++ to build Windows (.NET)
applications elegantly and efficiently.


Regards,

Ioannis Vranos
Jul 22 '05 #7
André wrote:
Hi,

is it possible to override methods for one specific object, like you can
do in Ruby? Something like:

--8<--

class A {
public:
void method();
}

void A::method() {
// do something...
}

A x = new A();

void x::method() {
// do something else...
}

--8<--

Or anything close to that?

Thanks,

Andre'


You can use pointers to member functions, or define other "external"
functions or so something like:
class base1
{
// ...

public:
virtual void method() { ... }
// ....
};

base1 obj1;
// ...
class base2: public base1
{
public:
void method() { new definition }
};
base2 obj2;


Regards,

Ioannis Vranos
Jul 22 '05 #8
"Ioannis Vranos" <iv*@guesswh.at .grad.com> wrote in message
news:ca******** ***@ulysses.noc .ntua.gr...
Phlip wrote:
C++ is for big, low-level
systems; not for the average code written today.

What's that suppose to mean? I use C++ to build Windows (.NET)
applications elegantly and efficiently.


Wrong. You use Managed C++ to build Windows .NET applications.

Mike Austin

Jul 22 '05 #9
Mike Austin wrote:
"Ioannis Vranos" <iv*@guesswh.at .grad.com> wrote in message
news:ca******** ***@ulysses.noc .ntua.gr...
Phlip wrote:

C++ is for big, low-level
systems; not for the average code written today.

What's that suppose to mean? I use C++ to build Windows (.NET)
application s elegantly and efficiently.

Wrong. You use Managed C++ to build Windows .NET applications.


:-) What do you mean? The so called "managed extensions" are
system-specific extensions for the specific platform. For example for
X-Windows Applications you can use the QT API. For Windows you can use
Borland's CLX/VCL.


Regards,

Ioannis Vranos
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1653
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these applications need to serialise the classes to/from the same files but only the GUI app needs the full range of class methods. Now, the rendering app needs to be ported to multiple OS's but the GUI doesn't. In order to reduce the time/cost of porting I'd...
2
1538
by: JJ L. | last post by:
Hello. I have a project that consists of nine different objects, each serving their own purpose. In the past I have just created a form for each one, and then whenever you call, say, object.Display(), it would call up the form associated with that object. This form only displays information, it doesn't allow the user to edit any information. Is it possible to find the properties of a certain object, and then loop through creating labels...
3
1870
by: Ken Varn | last post by:
I am just starting the process of creating ASP.NET server controls. I have created controls for .NET applications, but have just started with ASP.NET. I am a little confused about some areas that I am hoping someone can help clear up. 1. What is the difference between initializing a control in the constructor, vs the OnInit(), vs the CreateChildControls() methods? 2. The control that I created contains an Items collection attribute...
9
4650
by: Niels Jensen | last post by:
Hi All, I'm desperately looking for help regarding the following: I need to make a hexmap in it's own scrollable window on a form, when I say hex map I mean a graphical hexagon surrounded by other hexagons the same size. The program I'm writing imports region information i.e 10,13 would be a hexagon, 10 hex columns to the right and 13 hex rows down from hex 1,1.
7
1350
by: Brett Romero | last post by:
I need a static version of a class that can be referenced anywhere as a singleton and the same class that can be used as instances. Can this be done without basically creating the same class twice (one with static methods and one without)? Thanks, Brett
26
5373
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
6
3884
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display my array of objects in a GUI. How do I get JLabel to refer to the data in my objects? I've read my textbook and some tutorials online I just cannot get this. Plus all the examples I've seen are creating the information that will be displayed from scratch, while I have to use my previously created classes and add a GUI to it. I'm trying to do this GUI using JLabels but it won't let me refer to my CD class methods that...
6
2746
by: pbd22 | last post by:
Hi. I just got a project that is taking me into previously uncharted territory. I have a reasonably large VB6 project and need to "turn it into a web service". I know this isn't immediately possible. The two main options seem to be:
13
12802
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new scripting language that makes writing automated tests simpler. Really, I'm looking to kind of abstract the power of the C# language into a simpler language that's easier to learn. The script files would be interpreted by a script interpreter...
3
7429
Kelicula
by: Kelicula | last post by:
This is NOT a complete OO perl tutorial However I thought it could be beneficial to explain some of the basic concepts, and allow some users to simplify the software design process. I have found it to be extremely useful and beneficial to create my own "custom" modules. It (among many other things) allows you to type less. Or to make your keystrokes matter more, however you look at it. (If you do even look at it.) Things the reader...
0
8994
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8247
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.