|
Prev: Old fashion function definition?
Next: Design question
From: Andrew Falanga on 18 Dec 2007 11:01 Hi, I've got a program which with a class structure similar to the following: class A class B : public A class C : public B Now, there is a function in class A that must make use of a couple of functions defined in class B. Before I look at redesigning this code (because this may be poor design), I'd like to know how one can pass a reference to a derived class. Basically, class C calls the public methods of class A and one of these methods (in A) needs a reference to an object of type B. I tried simply doing this: PublicMethodInC( somevalue, *(this) ); but obviously this didn't produce what I needed. I then tried something like this: PublicMethodInC( somevalue, *(this->classB) ); which the compiler totally didn't like. Actually, I take that back. I think it was the linker that complained. (I just checked the output one more time. I'm still getting used to this M$ stuff, to all the M$ fans in this group, forgive me, but gag me.) As I think of it though, I could just send in a reference to the class C object since the B object is derived. From there, the compiler and linker should link me to the functions I need in B. I'd still appreciate some suggestions. I hate to admit to it, but I think I'm going to be redesigning some things. It does seem like poor design. Andy
From: Alf P. Steinbach on 18 Dec 2007 11:11 * Andrew Falanga: > > I've got a program which with a class structure similar to Please see the FAQ item on how to post a question about code that doesn't work, the repost. Cheers, & hth., - Alf -- 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?
From: Jim Langston on 18 Dec 2007 12:23 Andrew Falanga wrote: > Hi, > > I've got a program which with a class structure similar to the > following: > > class A > class B : public A > class C : public B > > Now, there is a function in class A that must make use of a couple of > functions defined in class B. Before I look at redesigning this code > (because this may be poor design), I'd like to know how one can pass a > reference to a derived class. Okay, this is a problem. When class A is defined it doesn't know about class B because it's derieved, doesn't exist yet. If class A needs to call a method in class B, then class B has to be defined first, but class B needs to have the definition of class A. You have a recursion problem. What you should probably do, then, is have the method in class A that needs to know about class B, put it in class B. Since you have to have class B defined to call it, it belongs in class B. It most likely needs to be a pure virtual class. Need to see what it is you're trying to do though. > Basically, class C calls the public methods of class A and one of > these methods (in A) needs a reference to an object of type B. I > tried simply doing this: So have class C call the public methods of class A, and the one that needs a reference to class B, put it into class B. > PublicMethodInC( somevalue, *(this) ); > > but obviously this didn't produce what I needed. I then tried > something like this: > > PublicMethodInC( somevalue, *(this->classB) ); > > which the compiler totally didn't like. Actually, I take that back. > I think it was the linker that complained. (I just checked the output > one more time. I'm still getting used to this M$ stuff, to all the M$ > fans in this group, forgive me, but gag me.) > > As I think of it though, I could just send in a reference to the class > C object since the B object is derived. From there, the compiler and > linker should link me to the functions I need in B. > > I'd still appreciate some suggestions. I hate to admit to it, but I > think I'm going to be redesigning some things. It does seem like poor > design. Need a little more information on what you are trying to do, but it sounds like whatever that call is in class A you need to call from C, make pure virtual in A, define it in B. Class A can not get instancized, but class B or C can. -- Jim Langston tazmaster(a)rocketmail.com
From: Andrew Falanga on 18 Dec 2007 12:27 On Dec 18, 9:11 am, "Alf P. Steinbach" <al...(a)start.no> wrote: > * Andrew Falanga: > > > > > I've got a program which with a class structure similar to > > Please see the FAQ item on how to post a question about code that > doesn't work, the repost. > > Cheers, & hth., > > - Alf > I kind of thought you were asking for code. Next time, please identify which FAQ you expect people to read. I couldn't find a question in the FAQ for this group (the only link I have for it is http://ma.rtij.nl/acllc-c++.FAQ.html), I had to go to the FAQ for comp.lang.c++ (http://www.parashift.com/c++-faq-lite/). Did this group, at some time, stop maintaining their own FAQ since the C++ FAQ- Lite is pretty well maintained? Because of where I work, I cannot post the code here. So, here goes in a skeleton program: #include <iostream> #include <windows.h> #include <winsock2.h> #include <ws2tcpip.h> #include <cstdlib> // forward declaration of class B class B; class A { SOCKET sd; sockaddr sa; int saLen = sizeof(sockaddr); public: void OpenConnection(); int TransferData( B& bObj, char* s , int len ) { int dataSent; if( (dataSent = send( sd, s, len )) < 0 ) { std::cerr << "error in send" << std::endl; bObj.IncrErrors(); } return dataSent; } }; class B : public A { static int totalErrorCount; int myErrorCount; public: void IncrErrors( int incrBy = 1 ) { totalErrorCount = myErrorCount += incrBy; } }; class C : public B { char* str; int length; public: C() : str( "Transfer this string to somewhere" ) { length = strlen( str ); } void TestConnection( ) { OpenConnection(); TransferData( *(this), str, length ); } }; I know that *(this) refers to this object which in the case I'm using it refers to the object C, but I was thinking that because it's derived from object B that it would work. At any rate, I'm still thinking that some redesign is in order because it seems to me that it's poor design that leads to objects of type A having to do work that B should do. Andy
From: Andrew Falanga on 18 Dec 2007 12:51
On Dec 18, 9:11 am, "Alf P. Steinbach" <al...(a)start.no> wrote: > * Andrew Falanga: > > > > > I've got a program which with a class structure similar to > > Please see the FAQ item on how to post a question about code that > doesn't work, the repost. Well, as happens to me many times, I must now eat a little bit of crow. I was misinterpreting the linker error (which was an unresolved symbol). The unresolved symbol that the work "this" in it and from that I was deducing that my calls to this, as referenced in my last posting, were causing the problem. However, what was causing the problem was that there was one function I had somehow managed to skip over defining. So, in fact, there was an unresolved symbol because the symbol didn't even exist (except as a declaration in the class definition). It hurts to be humbled. Andy |