seperating is really a work for months because this is somewhere in a 10x hierarchie and 100x parallel used chaotic class inheritance... A { getCond() astract }; <- B <- C <-D ... about 10 Steps A <-B1 ... A <- B2 ... and one of these C´s contains the thing with condUpdate() I inherited this code ... hating it .. maybe mutual or const_cast can help? but still update() is a non const function I found another solution now which can work with the "non const" version which is used in the code... But cleaning up this will be needed somedays..
FriendOfAsherah
Posts
-
const "getter" but updating on request? -
const "getter" but updating on request?Looking for an idea solving following problem: I have a "get()" function which returns the condition of an object stored in a member variable. There is also some code that updates this _cond
int getCond() const { return _cond; }
void UpdateCond() { ... etc ... _cond=f(); ... }In the whole code getCond() and UpdateCond() called are independetly, so that a "const" function could call "getCond()" But there is one silly stupid use of the getCond() where the Update() is done in the getCond() code So the former old coder maybe thought "update on request!"
int getCond() { UpdateCond(); return _cond; }
because UpdateCond() changes the member _cond getCond() cannot be const anymore. Anyone an idea to overcome this problem? (if not I have to change back this crunchy design into non-const getCond() functions... There are about 100 in derived classes ...
-
how to be sure each child class inherits a method?The structure is: CBase <- C1 <- C2 <- ... etc so straigt inheritance CBase contains the main method CBase::F() which does all calculations needed. And this method F() calls P() which should be choosen by inheritance (polymorphism) CBase::F() { ...; P(); ... } If I have CBase* b = new C2() than C2::P() will be called. If I have CBase* b = new C1() than C1::P() will be called. because of P() is a virtual method, overridden by C1::P().. C2::P() etc now I miss C1::P() (forgot to add it) => if CBase b=new C1() then CBase::F() will call CBase::P() instead of C1::P() because there is no C1::P() the override clause also tells the compiler to check if C1::P(), C2::P() match their parents P(). All is cointained by a complex chaotic SW-project which I will clean by small steps. The idea behind: P() is a call to a factory class which creates objects dependend on parameters AND on the class from which is called CBase::P(int type) creates objects of CPbase_ so CPbase_1, CPbase_2... C1::P(int type) creates obejcts of CP1_ so CP1_1, CP1_2... C2::P(int type) creates obejcts of CP2_ so CP2_1, CP2_2... etc... CBase::F(switch,type) : if (switch) P(type) else CBase::P(type) ... So if switch is set call the factory "of the class", else call the factory of CBase example: Cbase b = new C1(); b->F(true, 3) will create an object of C1_3() All the pointers are stored in a list and for each element in the list the creation-funbtion F() is called list ... C1*,CBase*,C3* etc ... foreach element in list , element->F(switch,type) ...
-
how to be sure each child class inherits a method?does not work ... I can ommit P() in C2 without compile error because its stil defined in C1 also I miss the code of CBase::P() itself like:
class CBase
{
private:
virtual void P() abstract; //{ printf("CBase::P\n"); } // <= and whats about that code??
public:
void F() { printf("Base: F()->P(): "), P(); }
};class C1 : public CBase
{
private:
void P() override { printf("C1::P\n"); }
};
class C2 : public C1
{
private:
//void P() override { printf("C2::P\n"); }
}; -
how to be sure each child class inherits a method?in which class, where to put this? in CBase? -> then CBase doesnt have its P() method itself in struct PI? ->still used this "=0" is same systax like "abstract"
-
how to be sure each child class inherits a method?I have a straight class hierarchy. The base class only has a method F() which calls another method P(). This method P() has to be implemented in all of the child classes and is called by polymorphism. How can I get sure (at compile time) that all child classes implement this method P()?
class CBase
{
private:
virtual void P() { printf("CBase::P\n"); }
public:
void F() { printf("Base: F()->P(): "), P(); }
};class C1 : public CBase
{
private:
virtual void P() override { printf("C1::P\n"); }
};
class C2 : public C1
{
private:
virtual void P() override { printf("C2::P\n"); }
};void main()
{
CBase* p = new CBase();
p->F();
p = new C1();
p->F();
p = new C2();
p->F();
}The above is the structure, when all is correct and will result in: Base: F()->P(): CBase::P Base: F()->P(): C1::P Base: F()->P(): C2::P Now I made a mistake and forgot to implement P() in class C1
class C1 : public CBase
{
private:
//virtual void P() override { printf("C1::P\n"); }
};the compiler doesn´t error and it will result in Base: F()->P(): CBase::P Base: F()->P(): CBase::P Base: F()->P(): C2::P The only Idea is to use an Interface declaring P() abstract. But since C2 is derived from C1 from CBase how to do? Followeing code will show up an error on missing P(), but also shows up warnings when all is correct: _warning C4584: 'C1': base class 'PI' is already base of 'CBase' warning C4584: 'C2': base class 'PI' is already base of 'C1'
struct PI
{
virtual void P() abstract;
};class CBase : public PI
{
private:
virtual void P() { printf("CBase::P\n"); }
public:
void F() { printf("Base: F()->P(): "), P(); }
};class C1 : public CBase, public PI
{
private:
virtual void P() override { printf("C1::P\n"); }
};
class C2 : public C1, public PI
{
private:
virtual void P() override { printf("C2::P\n"); }
};also it doesnt help at all because now I have to be sure to derive C1,C2, ... from PI too, not only from its parent So what I need is "method P() has to be declared in all child classes!- error if it is not"_
-
variadic templates problemIm using this for the first time for mapping a function "f" with variable count of parameters declarition:
template void AllItemsO(void (*f)(A...), A... args)
{
// for (auto _F : *this)
// if ((_F)) ((_F)->*f)(args);
}call in cpp:
AllItemsO(&IcompDWO3D::SetObjMat, visInfo, pci, doit, obj);
function to call is: IcompDWO3D::SetObjMat parameters: visInfo, pci, doit, obj compiler always shows up an error C2784 Fehler 1 error C2784: "void CapDWOICollection::AllItemsO(void (__cdecl *)(A...),A...)": template-Argument für "void (__cdecl *)(IapVisibilityInfo *,IPickCookie *,aprivis::MakeSubObjSpecial,aprivis::avhObjectWrapper &,A...)" konnte nicht von "void (__thiscall component::IcompDWO3D::* )(IapVisibilityInfo *,IPickCookie *,aprivis::MakeSubObjSpecial,aprivis::avhObjectWrapper &)" hergeleitet werden. looking at what I have shown bold, it seems that it cannot spread the functionparameters form the A... variadic to single paarameters? A non variadic verison with 2 Parameters looks like this and works:
template void AllItemsO2(void (*f)(T1, T2), T1 p1, T2 p2)
{ for (auto _F : *this) if (_F) ((_F)->*f)(p1, p2); }any help for my syntax?
-
__interface keyword not practicable when derived class exports to dll?I found out that
__interface I { };
class __declspc(dllexport) C: public I
{
}does not work because C then needs the assigment operator I::operator =() when compiler generates default implementation of C::operator =(const C&) I can overcome this implmenting a C::operator= but whats the reason of implementing operator = for dllexport? any other way to solve this?
-
error 2039 {dtor} is not element of ImyInterface - when does this happen?found out the bugmyself now I used the virtual keyword in the declaration
class MyClass : virtual public ImyInterface, public CBase
removing the virtual keyword makes the error disappear :)
-
error 2039 {dtor} is not element of ImyInterface - when does this happen?I have declared a simpl interface using
__interface ImyInterface
{
virtual Method1() abstract;
virtual Method2() abstract;
};
class MyClass : public ImyInterface, public CBase
{
public:
MyClass();
virtual ~MyClass();
virtual Method1();
virtual Method2();protected:
int member1, member2;
};This is implemented in big code where I use downcasts like
CBase* ptr = new CMyClass();
dynamic_cast(ptr);and some other typecasting of ImyInterface* found only by "search complete project" on one hand destructors are not allowed in an Interface because __interface uses __declspec(novtable) on the other hand the compiler wants to have a destructor (error is shown at "}" of the derived class) What constructs in the code will force the compile to raise this error, and, is the usage of __interface in c++ too special for interfaces and I should use struct only?. In common: when never use interface as a base of a class?
-
Overloading methods with identical content in lot of derived classesyes, they are different or use one member in this functions that is diffrent. The functions to add itself are same to all, but refer to prior defined methods, functions or class members macros is the :wtf: WORST madhog (method) ever. I know this because im now in task to remove all these mass of macros (1 - 10 pages long) not debugable, now intllisense, you wont find anything. in addition the advantage of OOD /OOP has been destroyed with these macros because a lot of not used things were copied because the developer didn´t know whats in there anymore (stupid copying) Ther must be a smarter better way, hopefully
-
Overloading methods with identical content in lot of derived classesan example: We have animals, cats and dogs
class animal()
{
OpenMouth() ;
breath_in();
breath_out();
}
class cat() : public animal
{
play_with_wool();
}
class dog() : public animal
{
hunt_cats();
}
class mops1() : public dog
{
get_fat_and_fart()
}
class mops2() : public dog
{
get_fat()
}
class greyhound1() : public dog
{
run()
}
class specialdog() : public dog
{
run_fast()
}now we want to extent _some_ dogs to know how to barf() for barf() we need some unctions of animal() (not all)
barf() { breath_in(); OpenMouth(); dosomething(); }
How can we add barf() to some (eg. 20 different dogs, and also some wolfs) without changing parent class structure?
class greyhound1() : public dog
{
run()
barf(); // added ...
}
class specialdog() : public dog
{
run_fast()
barf(); // added ...
}
class greywolf() : public wolf
{
barf(); // added
}and next step: we have not only barf() we have 20 new extends for these aimals. It would mean: add these 20 functions to each animal seperately?
-
Calling a method with the pointer to a base classoh , the example I have is more tricky: D should call "the next M() of any parent above the child class C1" Its vice versa to the virtual inheritance when you call the last one in the hierarchy ... if I call
C1::anytest ()
{
B::M();
}it goes "up" the hierarchy an calls the next found M(); in this case A::M() But in class D´s DoSomething where I get a ptr to C1, or C2 ... how can do it there?
-
Calling a method with the pointer to a base classI´m sure there is a simple solution but it drives me crazy:
class A
{ virtual bool method M() };class B : public A { .... }
class C1 : public B
{ virtual bool method M() override };
class C2 : public B
{ virtual bool method M() override };class D
{
static bool DoSomething( A* ptr ) { ptr -> M(); }
}Now I am working in a codepiece of C1 which calls D::DoDomething( )
C1::anymethod()
{
D::DoSomething( this );
}Ok, thats simple: C1::M() will be called because of the inheritance But now I wanna have that D calls the M() of C1´s base A by typecasting the this ptr to a base:
C1::anymethod()
{
D::DoSomething( (A*)this );
}but this will also call C1:M(). How can I tell the D::DoSomething function "If you use M1() then you have to call A::M1() " with knowing that in DoSomething by the type of the ptr?
-
Overloading methods with identical content in lot of derived classesmaybe I got now what you talk about "Changing the class is not possible when transfered via serial... because of reconstruction is dependent of class structure"? OK, this doesnt happen here, there is an extra struct that holds data to store and load. What I know is, that the code was extended within years, adding more and more features to the software. The derived classes have more features and do some different calculations e.g. B::M1() ... used in B, Ca1, Cb1 also Ca2, Cb2 ... ect is different to Cc1::M1(), Cc2::M1() ... etc.
-
Overloading methods with identical content in lot of derived classesProblem is inheritance to base of Cc1 Cc2 etc B<-Ca1<-Cb1<-Cc1 B<-Ca2<-Cb3<-Cc2 if I use an intermediate class e.g D and declare D:M1() then following problem arises: class B { virtual M1() }; class Ca1 : public B class Cb1 : public Ca1 class D : { virtual M1() } ; class Cc1: public Cb1 , public D class Cc2 : public Cb2 , public D ... if I call now x->M1() with x is a Cc1* which method will be used? Does D::M1() overload B::M1(), what I like to have?
-
Overloading methods with identical content in lot of derived classes?? stream? don´t understand because I don´t us a stream but do ou mean, that these horrible MFC macros came up because of th same problem?
-
Overloading methods with identical content in lot of derived classessorry its tricky but: Cc1::M1() is identical to Cc2::M1() ... the only "data" some Mi() uses is the "this" pointer Cc1::M1() { code m1 } Cc2::M1() { code m1 } ... etc.. Cc1::M3() { f_calculate(this); ... } Cc2::M3() { f_calculate(this); ... } ...
-
Overloading methods with identical content in lot of derived classesMapping means: to copy the same method mapping declaration to each class Cc1 ... Ccn so class Cc1: { ... list of M1() ...Mn()- mappings } class Cc2: { ... list of M1() ...Mn() mappings } .... class Ccn: { ... list of M1() ...Mn() mappings } this is lot of code ... isn´t there any way to say: class Cc1..n - overload "the punch of M1...Mn defined in a common set" layer in between does not work because: - Cc1...Ccn is derived from Cb1...Cbn - Cc_::M1() used some data of Cc_
-
reguired fast printingThis is very general question :-D Think about following topics: - printer connection via: direct connection on parellel Centronics, ... WLAN network - communication: whats between printer and programm code? direct commnication, printer driver, spooler, LAN network... - how does operation system drivers communicate? do you use "Windows" printsystem which does Scaling, function mapping from window->draw to printer->draw? the more comfortable you can code the slower the speed. fastestway: use ASM and direct code the 8x8 bitmaps, send them to the printer, as we did in 1985 but think of: paper size, drawing, filling, fonts etc etc.