473,325 Members | 2,774 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,325 software developers and data experts.

Overload by deriv class param; call w base class param

Abstract base class A. Derived from A are class B and C, both concrete.

I hold a pointer to an object of class A, an instance which is either B
or C.

I have trouble with making the following work, while I think it should
be possible:

A *pA = new B; // One of...
A *pA = new C; // ...these two
DoSomething(pA); // Call overloaded method

This called method is overloaded, and looks like this:

void DoSomething(B *pB)
{
// Code to handle a B
}

void DoSomething(C *pC)
{
// Code to handle a C)
}

If this is not possible, what should I do? Is the following the only
way?

void DoSomething(A *pA)
{
if (pA->IsTypeOf(B))
// Code to handle a B
else if (pA->IsTypeOf(C))
// Code to handle a C
}

There must be something more "OO".

Thanks,
E.

Jul 25 '05 #1
12 1604
* ectoplasm:


Make DoSomething a virtual member function of class A.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 25 '05 #2
Alf P. Steinbach, thanks. But my example simplified things and I forgot
to mention: the methods I want to implement shall not be (virtual)
members of A. Are there other solutions?

Jul 25 '05 #3
* ectoplasm:
Alf P. Steinbach, thanks. But my example simplified things and I forgot
to mention: the methods I want to implement shall not be (virtual)
members of A. Are there other solutions?


Depends what the problem is.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 25 '05 #4
The problem is, that MS VC++ gives a compile error

error C2664: cannot convert parameter 1 from 'A *' to 'B *'

on my call DoSomething(pA). Even though pA is a pointer to a B
instance.

Seems there is no way around this. A method can only take a parameter
of its type or a derived type. In other words, can only be an
(implicit) upcast. In my opinion, this is a failure of C++. Don't you
agree that what I want is not strange at all but is OO functionality
that naturally should be possible?

Jul 25 '05 #5
On 24 Jul 2005 20:11:43 -0700, "ectoplasm" <e_*****@hotmail.com> wrote
in comp.lang.c++:
Alf P. Steinbach, thanks. But my example simplified things and I forgot
to mention: the methods I want to implement shall not be (virtual)
members of A. Are there other solutions?


Why not?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 25 '05 #6
On 24 Jul 2005 21:50:04 -0700, "ectoplasm" <e_*****@hotmail.com> wrote
in comp.lang.c++:
The problem is, that MS VC++ gives a compile error

error C2664: cannot convert parameter 1 from 'A *' to 'B *'

on my call DoSomething(pA). Even though pA is a pointer to a B
instance.

Seems there is no way around this. A method can only take a parameter
of its type or a derived type. In other words, can only be an
(implicit) upcast. In my opinion, this is a failure of C++. Don't you
agree that what I want is not strange at all but is OO functionality
that naturally should be possible?


No, it is not strange. C++ provides a simple method of doing it,
providing a virtual function in the base class. What is silly is your
arbitrary insistence that you must have functionality but won't use
the standard mechanism that provides it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 25 '05 #7
* ectoplasm:
The problem is, that MS VC++ gives a compile error

error C2664: cannot convert parameter 1 from 'A *' to 'B *'

on my call DoSomething(pA). Even though pA is a pointer to a B
instance.
That is not the problem, it's the compiler telling you you've chosen a bad
way to solve the problem.

Seems there is no way around this. A method can only take a parameter
of its type or a derived type. In other words, can only be an
(implicit) upcast. In my opinion, this is a failure of C++. Don't you
agree that what I want is not strange at all but is OO functionality
that naturally should be possible?


What you seem to want is not OO, it's the opposite.

However, my earlier comment stands: make 'DoSomething' a virtual member
function of class A. You then wrote, "the [two] methods I want to implement
shall not be (virtual) members of A", but that's OK. Here's one way --
and note that it's just a technical solution, it _does not_ solve the real
problem, which is at the design level, not the language level, and unknown:

#include <iostream>
#include <ostream>

class B;
class C;

static void doSomething( B* )
{
std::cout << "doSomething( B* )" << std::endl;
}

static void doSomething( C* )
{
std::cout << "doSomething( C* )" << std::endl;
}

class A
{
private:
virtual void doSomething() = 0;
public:
static void doSomething( A* arg ) { arg->doSomething(); }
};

class B: public A
{
private:
virtual void DoSomething()
{
::doSomething( this );
}
};

class C: public A
{
private:
virtual void doSomething()
{
::doSomething( this );
}
};

void doSomething( A* arg )
{
A::doSomething( arg );
}

int main()
{
A* p = new C;
doSomething( p );
delete p;
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 25 '05 #8
With reference to the line:

DoSomething(pA); // Call overloaded method

Let us say the compiler should do it. Then, which one of the two
overloads should it choose?

I am trying to say that the compiler cannot resolve the ambiguity, even
if it did implicit upcast of some sort.
Hope that helps.
Z.

Jul 25 '05 #9
Even though it's been a few days, I still want to comment on this
because I think it still is like a "missing link" in C++ / OO; your
arguments are not convincing me.

You ask: why shall the method (DoSomething()) not be a virtual member
of A? Answer: the method I want to call is actually a constructor of a
dialog (CDialog class of MFC). You'll have to agree that it is not good
design to have dialogs (UI elements) inside of a class which abstracts
some entity (clear separation between data and visualisation). So I
want to be able to have a dialog for a B and C class instance; they
have a different appearance. Suppose the dialog class is simply named
Dialog:

class Dialog
{
Dialog (B *pB);
Dialog (C *pC);
~Dialog();
}

I want to hold a pointer to type A and when I construct the dialog, I
don't want to know whether it is really a B or C (derived) type. Only
at the moment a dialog needs to be shown, their difference becomes
apparent (inside of the dialog).

Then, Zorro, you say the compiler cannot know which overload to choose.
Right, but what about virtual calls, the vtable...? To make the above
work, the only thing the compiler might want to check if the method
DoSomething() has overrides for all types derived from A. Maybe also
have a default implementation at the base level (A).

Concluding, I wouldn't know why there cannot be overloads of (non
member) functions based on the parameter's common base class. You say
it is not OO, I say it definitely is. It is not a far-fetched idea and
you can easily find real world analogies for this situation.

Jul 27 '05 #10
N.b.: the dialog constructor is analogous to the DoSomething() method;
I mixed them up in my last post. Sorry for that.

Jul 27 '05 #11
* ectoplasm:
N.b.: the dialog constructor is analogous to the DoSomething() method;
I mixed them up in my last post. Sorry for that.


Well you have already got working code to solve your language-level problem,
so what is the fuzz all about?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 27 '05 #12
> Then, Zorro, you say the compiler cannot know which overload to choose.

All I am saying is the following (using your class example).

Given that p is a pointer to base, what is expected from the line:

Dialog MyDiag(p);

Forget about the compiler. What would you do? Would you create an
instance using type B or C?

Regards,
zo****@ZHMicro.com
http://www.zhmicro.com
http://distributed-software.blogspot.com

Jul 28 '05 #13

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

Similar topics

6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
10
by: Benny Raymond | last post by:
I'm trying to change the way a treeview works just a bit, i'm pretty new to C# and now i'm running into overloading. I tried the following code and it's yelling at me saying that no overload...
19
by: Dave Raskin | last post by:
public class Base { } public class Derived : Base { } public class Service {
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
3
by: needin4mation | last post by:
The code is taken from the book Professional C#: abstract class GenericCustomer { private string name; public GenericCustomer(string name) { this.name = name; }...
5
by: Dimitry | last post by:
I am trying to make the following: struct Base { char param; }; class Derived1 : public Base { public:
4
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
2
by: dolphin | last post by:
Hi everyone . I am confused about the different between override overload and hide. May be I have a wrong opinion, I always think that hide is very similar with override. Both of them call the...
4
by: l.s.rockfan | last post by:
Hello, how do i have to call an inherited, templated class constructor from the initializer list of the inheriting, non-templated class constructor? example code: template<typename T>...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.