Adding a member function outside class definition

C

carlosp

I am starting with C++, coming from fortran90. I am learning classes,
and I want to start building a "real life" program. I need to
manipulate matrices, diagonalize etc, so I am making use of it++, and
I have a question coming from there, but completely independent of it.

Let say I have some header files defining some class, and its member
functions, I want to add a new member function _without_ changing the
code of the other files. I have tried various combinations without any
success Of course I could define a routine that does a similar job,
but I would like to call it in the

object.method()

style. Is there a way to do it??? I am sorry if this is to trivial, I
am stuck in here! I have been reading Stroustrup, and probably inside
it is the answer but it is too long and I want the answer soon!

Thanks a lot!

Carlos
 
L

ltcmelo

I am starting with C++, coming from fortran90. I am learning classes,
and I want to start building a "real life" program. I need to
manipulate matrices, diagonalize etc, so I am making use of it++, and
I have a question coming from there, but completely independent of it.

Let say I have some header files defining some class, and its member
functions, I want to add a new member function _without_ changing the
code of the other files. I have tried various combinations without any
success Of course I could define a routine that does a similar job,
but I would like to call it in the

object.method()

style. Is there a way to do it??? I am sorry if this is to trivial, I
am stuck in here! I have been reading Stroustrup, and probably inside
it is the answer but it is too long and I want the answer soon!

You cannot do that in C++. You need to access the class code to create
a new member.
 
S

Salt_Peter

You cannot do that in C++. You need to access the class code to create
a new member.

Thats incorrect. He's looking to add features to the original class
without modifying it.
Declare and define a class derived from the Original class, add
whatever features you want to the derivative.

class Original { ... }; // can't touch this

class Derived : public Original
{
public:
void method() { ... }
};

int main()
{
Derived obj; // invokes compiler-generated default ctor
obj.method(); // obj is_an instance of Original type
}
 
L

ltcmelo

Thats incorrect. He's looking to add features to the original class
without modifying it.
Declare and define a class derived from the Original class, add
whatever features you want to the derivative.

class Original { ... }; // can't touch this

class Derived : public Original
{
public:
  void method() { ... }

};

int main()
{
  Derived obj; // invokes compiler-generated default ctor
  obj.method(); // obj is_an instance of Original type



}

Well, your code is correct. However, in my opinion this is not exactly
what was asked for (perhaps I misunderstood, I thought of some kind of
"member injection"). Anyway, if this was the intention of the OP,
good!
 
C

carlosp

Well, your code is correct. However, in my opinion this is not exactly
what was asked for (perhaps I misunderstood, I thought of some kind of
"member injection"). Anyway, if this was the intention of the OP,
good!

Well I was looking for a solution. Though I would like to keep the
original class name and so on, I think what is closest to my need is
the answer posted by Salt_Peter! Thanks a lot!

Carlos
 
E

EventHelix.com

I am starting with C++, coming from fortran90. I am learning classes,
and I want to start building a "real life" program. I need to
manipulate matrices, diagonalize etc, so I am making use of it++, and
I have a question coming from there, but completely independent of it.

Let say I have some header files defining some class, and its member
functions, I want to add a new member function _without_ changing the
code of the other files. I have tried various combinations without any
success Of course I could define a routine that does a similar job,
but I would like to call it in the

object.method()

style. Is there a way to do it??? I am sorry if this is to trivial, I
am stuck in here! I have been reading Stroustrup, and probably inside
it is the answer but it is too long and I want the answer soon!

Thanks a lot!

Carlos

You cannot do this in C++. C# 3.0 lets you define such extension
methods.

http://en.wikipedia.org/wiki/Extension_method
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top