Search found 4039 matches

by albinopapa
March 30th, 2020, 1:18 am
Forum: Everything
Topic: List of subclass pointers...
Replies: 12
Views: 69

Re: List of subclass pointers...

Just wanted to say it's a shame you aren't using std::variant. It's a pretty decent alternative to dynamic polymorphism. Here's a sample of finding an object by type using it: #include <variant> class B0 { }; class B1 { }; class A { public: // List each sub-class type in the template parameter list ...
by albinopapa
March 30th, 2020, 12:56 am
Forum: Everything
Topic: std::string::operator+(int) ?
Replies: 13
Views: 53

Re: std::string::operator+(int) ?

Just wondering about your use of "lisibility", do you mean "legibility", "usability" or "visibility" or something else?
by albinopapa
March 30th, 2020, 12:52 am
Forum: Everything
Topic: std::string::operator+(int) ?
Replies: 13
Views: 53

Re: std::string::operator+(int) ?

Make a non-member function overload:

Code: Select all

std::string operator+( std::string const& str, int num )
{
     return str + std::to_string( num );
}
by albinopapa
March 29th, 2020, 10:06 am
Forum: Everything
Topic: List of subclass pointers...
Replies: 12
Views: 69

Re: List of subclass pointers...

Hehe, yeah, having an enum or something is one way of determining the type as well. To use the visitor pattern ( the one I used in previous post ) you need to visit the class to get it's type. This makes the compiler look up the function pointer in the vtable then calls that function. Once in the me...
by albinopapa
March 29th, 2020, 4:47 am
Forum: Everything
Topic: List of subclass pointers...
Replies: 12
Views: 69

Re: List of subclass pointers...

class B0; class B1; class B2; class A { public: void f( A* pObj ) { pObj->visit(); } protected: virtual void visit() = 0; void DoWork( A*, A* ) {} void DoWork( B0* ){ /* Do whatever f(A*) was going to do with A* */} void DoWork( B1* pObj ) { if(pObj->Lives()<=0)/* kill object */; } void DoWork( B2*...
by albinopapa
March 29th, 2020, 12:59 am
Forum: Everything
Topic: List of subclass pointers...
Replies: 12
Views: 69

Re: List of subclass pointers...

That being said though, that's only if the data is private...but if you aren't setting things to private then once you get into the DoWork function with the actual B1/B2 type, then you will have access to their public data members since they are B1s and B2s and not just a polymorphic A*.
by albinopapa
March 29th, 2020, 12:57 am
Forum: Everything
Topic: List of subclass pointers...
Replies: 12
Views: 69

Re: List of subclass pointers...

No, you are correct, A will not be able to access B1::( data ) nor B2::( data ) unless you create accessors ( get/set that you loathe so much ). Ah, the joys of OOP and encapsulation.
by albinopapa
March 28th, 2020, 4:21 pm
Forum: Everything
Topic: List of subclass pointers...
Replies: 12
Views: 69

Re: List of subclass pointers...

Here's the explanation of what the second section of code does. Calling pObj->F( pObj2 ) will then call the virtual function visit() on whatever type pObj2 is. So if pObj2 is a B0*, then B0::visit() is called and if it's a B1* then B1::visit() is called. Once inside the visit() function, the base Do...
by albinopapa
March 28th, 2020, 4:13 pm
Forum: Everything
Topic: List of subclass pointers...
Replies: 12
Views: 69

Re: List of subclass pointers...

class A { public: virtual void F( A* obj ); }; class B0 : public A { public: // Doesn't need to define F() if A::F() handles behavior }; class B1 : public A { public: void F( A* obj ) override; }; class B2 : public A { public: void F( A* obj ) override; }; Calling objB1->F( objA ) will call B1::F( ...
by albinopapa
March 28th, 2020, 6:53 am
Forum: Everything
Topic: Sprites are kinda slow
Replies: 9
Views: 69

Re: Sprites are kinda slow

Different threads can use the same functions and read from the same variable just fine, it is as you say a bad thing to read and write to the same variable from different threads at the same time. What you have should be fine.