473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1724
On Fri, 18 Jun 2004 16:31:01 -0300 in comp.lang.c++, André
<an**********@syspoint.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(BarnyardAnimal& me, int food)
{
me.something = 7 + food; //Accessing member variable
}

public:

int (*pEat)(BarnyardAnimal&,int);

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

BarnyardAnimal(void) : Eat(EatDefault)
{

}

};
int main(void)
{
BarnyardAnimal cow;

cow.Eat(5); //Default function

cow.Eat = ReturnFunctionPointer();

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 = ReturnFunctionPointer();

TYPO TYPO TYPO

cow.pEat = ReturnFunctionPointer();


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(BarnyardAnimal& me, int food)
{
me.something = 7 + food; //Accessing member variable
}

friend int EatOther(BarnyardAnimal&, int);

public:

int (*pEat)(BarnyardAnimal&,int);

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

BarnyardAnimal(void) : pEat(EatDefault)
{

}

};

int EatOther(BarnyardAnimal& 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)
applications 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
Ioannis Vranos wrote:
Mike Austin wrote:

Ioannis Vranos wrote:
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.


:-) 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.


What /I/ mean is to write a GUI, or a Web site, or a bunch of business
rules, or glue between enterprise modules, you need a rapid and dynamically
typed language that stays out of your way.

C++ is fun and tricky because of all the ways to create undefined behavior.
Anyone who thinks one can efficiently develop while avoiding all these
issues is inexperienced with other modern languages. C++ is portable OO
assembler, and it always puts the needs of the CPU above those of the
programmer.

What Mike probably meant is "Managed C++" is not C++ because it uses p-code
to buffer (but not remove) all those undefined behavior problems.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #11
Phlip wrote:
:-) 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.

What /I/ mean is to write a GUI, or a Web site, or a bunch of business
rules, or glue between enterprise modules, you need a rapid and dynamically
typed language that stays out of your way.

The above does not mean anything in particular.
C++ is fun and tricky because of all the ways to create undefined behavior.
Anyone who thinks one can efficiently develop while avoiding all these
issues is inexperienced with other modern languages. C++ is portable OO
assembler, and it always puts the needs of the CPU above those of the
programmer.

C++ is occupies both the high level and low level space. And you can
choose the level of abstraction you like for your programming. For
regular applications running on top of an OS, you can state at the high
level of programming, where C++ has nothing to jealous from other
languages since it supports 4 paradigms and each one is supported well
(better than most one-paradigm programming languages out there).

So for example, instead of using a char * with new[] for strings, better
use std::string.

What Mike probably meant is "Managed C++" is not C++ because it uses p-code
to buffer (but not remove) all those undefined behavior problems.

Check these interesting things about C++ and .NET interaction:

There check the Visual C++ paragraph:
http://msdn.microsoft.com/vstudio/pr....aspx#language
This will become part of the future C++ standard:
http://www.ecma-international.org/news/ecma-TG5-PR.htm


Regards,

Ioannis Vranos
Jul 22 '05 #12
Ioannis Vranos wrote:
Check these interesting things about C++ and .NET interaction:

There check the Visual C++ paragraph:
http://msdn.microsoft.com/vstudio/pr....aspx#language
This will become part of the future C++ standard:
http://www.ecma-international.org/news/ecma-TG5-PR.htm

And you can download the draft standard from here:

http://msdn.microsoft.com/visualc/ho...a/default.aspx


Regards,

Ioannis Vranos
Jul 22 '05 #13
Ioannis Vranos wrote:
Phlip wrote:
What /I/ mean is to write a GUI, or a Web site, or a bunch of business
rules, or glue between enterprise modules, you need a rapid and dynamically typed language that stays out of your way.


The above does not mean anything in particular.


You understand dynamic typing, right?
C++ is occupies both the high level and low level space. And you can
choose the level of abstraction you like for your programming. For
regular applications running on top of an OS, you can state at the high
level of programming, where C++ has nothing to jealous from other
languages since it supports 4 paradigms and each one is supported well
(better than most one-paradigm programming languages out there).

So for example, instead of using a char * with new[] for strings, better
use std::string.
Bob Hairgrove wrote [in another thread]:
Since the base class destructor [of std::list<>] is
not virtual, it is not called when
your derived class is destroyed.


The most important resource to optimize is programmer time.

C++ always errs in favor of the CPU, not the programmer. In some languages
all destructors are virtual, and the overhead for each class object is high.
In C++ it is low. If you are programming a cell phone, use C++.

--
Phlip
Jul 22 '05 #14
Phlip wrote:
The most important resource to optimize is programmer time.

C++ always errs in favor of the CPU, not the programmer. In some languages
all destructors are virtual, and the overhead for each class object is high.
In C++ it is low. If you are programming a cell phone, use C++.

Also it favours space efficiency too. What about a heavy duty, non-cell
phone, server system where we want to utilize every cycle and byte of it?

Is writing the keyword "virtual", which is rarely needed in essence,
really that tiresome?


Regards,

Ioannis Vranos
Jul 22 '05 #15
Ioannis Vranos wrote:
Phlip wrote:
The most important resource to optimize is programmer time.

C++ always errs in favor of the CPU, not the programmer. In some languages all destructors are virtual, and the overhead for each class object is high. In C++ it is low. If you are programming a cell phone, use C++.
Also it favours space efficiency too. What about a heavy duty, non-cell
phone, server system where we want to utilize every cycle and byte of it?


A great way to write one of those is in a soft language, but then profile
where the big footprint and the time bottlenecks are. Fix the latter by
improving its algorithm, then converting it to a lower level language.

The 80/20 rule says that 80% of your bottlenecks are in 20% of your code, so
converting 20% of it to C++ will speed you up.
Is writing the keyword "virtual", which is rarely needed in essence,
really that tiresome?


If that were the only loophole...

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #16
In message <eq****************@newssvr19.news.prodigy.com>, Phlip
<ph*******@yahoo.com> writes

Bob Hairgrove wrote [in another thread]:
Since the base class destructor [of std::list<>] is
not virtual, it is not called when
your derived class is destroyed.


And he was incorrect, as he acknowledged.

--
Richard Herring
Jul 22 '05 #17
"Phlip" <ph*******@yahoo.com> wrote in message news:<El***************@newssvr33.news.prodigy.com >...
Ioannis Vranos wrote:
Phlip wrote:
The most important resource to optimize is programmer time.

C++ always errs in favor of the CPU, not the programmer. In some languages all destructors are virtual, and the overhead for each class object is high. In C++ it is low. If you are programming a cell phone, use C++.
Also it favours space efficiency too. What about a heavy duty, non-cell
phone, server system where we want to utilize every cycle and byte of it?


A great way to write one of those is in a soft language, but then profile
where the big footprint and the time bottlenecks are. Fix the latter by
improving its algorithm, then converting it to a lower level language.


That would be inpractical. One usually knows where are bottlenecks,
so, adequatly, project can be divided in soft:)/ hard:) parts, about
the time when project starts.

The 80/20 rule says that 80% of your bottlenecks are in 20% of your code, so
converting 20% of it to C++ will speed you up.


Why converting? This just wastes time and possibly introduces knew
bugs.
Greetings, Bane.

P.S. Once I'v wrote banking application using C++ builder,
faster then other two guys that used oracle forms. I was finished
long ago before they finished fighting with their tool in order to met
user requirements :)
Jul 22 '05 #18
Branimir Maksimovic wrote:
Phlip wrote:

A great way to write one of those is in a soft language, but then profile
where the big footprint and the time bottlenecks are. Fix the latter by
improving its algorithm, then converting it to a lower level language.


That would be inpractical. One usually knows where are bottlenecks,
so, adequatly, project can be divided in soft:)/ hard:) parts, about
the time when project starts.

The 80/20 rule says that 80% of your bottlenecks are in 20% of your code, so
converting 20% of it to C++ will speed you up.


Why converting? This just wastes time and possibly introduces knew
bugs.

Greetings, Bane.

P.S. Once I'v wrote banking application using C++ builder,
faster then other two guys that used oracle forms. I was finished
long ago before they finished fighting with their tool in order to met
user requirements :)


You probably carefully analyzed requirements, developed an object
model, and implemented it. Props. (That means "proper respects",
honestly.)

I once took a program written in absolutely despicable BASIC, all one
big function, and re-wrote it as a very small amount of Ruby code.

http://flea.sourceforge.net

I did it by converting the BASIC into a form that I could call with
system(""). Then I put the simplest possible input into that program,
collected the output, and wrote a test case which forced my Ruby code
to return the same output.

When the test failed for the correct reason, I wrote just enough Ruby
to pass the test. Then I wrote another test requesting the next
simplest feature, and I got that test to pass.

As the Ruby code grew, I attended to its design, and refactored to
develop its object model. While refactoring, I ran all the tests every
1~10 edits.

Today that program contains around 100 tests. While developing it, the
only bugs I found were in the immediate feature I worked on. Adding
new features did not cause bugs, and refactoring to improve the design
did not cause bugs. And improved designs resist bugs.

In my current work I am unfamiliar with the profile of bug risks that
you fear, but of course in the olden days I would never have
refactored my modules so aggressively. These days I use test-first
development to avoid operating a debugger, and am free to change my
designs at whim, including changing a module's language.

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

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

Similar topics

9
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...
2
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,...
3
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...
9
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...
7
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...
26
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...
6
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...
6
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...
13
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...
3
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.