« first day (1694 days earlier)      last day (1161 days later) » 

6:48 AM
I have a base class with some members and a derived class. I want some members of the base class to be used in the derived class; and I want some as a mandatory member for the derived class (the base class just declares it and the derived class defines it). I know how to do the first part (: public C). But I don't know how to do the other part.
 
 
2 hours later…
8:42 AM
Assuming the "part" you want the derived class to define is a member function, you define that member function as pure virtual in the base class.
class base {
public:
virtual void pv() = 0;
};

class derived : public base {
public:
virtual void pv() { /* whatever */ }
};
While it's possible to derive a class from base that doesn't override pv, you can't create an instance of that class--you have to create some further-derived class that does override it before you can create an instance.
 
@JerryCoffin thanks. that's what I was looking for. but still one unresolved issue. it works for functions but not variables.
 
@Milad Correct. There's no analog of it for variables either.
Depending on exactly what you're after, you might be able to use templates.

template <class T>
class foo {
T t;
// ...
};

Now to instantiate `foo`, the user has to supply a type, which will become the type of the member variable `t`.
 
8:59 AM
@JerryCoffin turned out that this is also a problem. I NEED to use the base instance (it represents a general type (of it's derived classes)); but get error with derived classes that do not implement the required members.
 
9:09 AM
My code looks like this:

// The base class
class Type_1X
{
void action1();
int value;
}

class Type_15
{
void action1() {}
int value = 3;

// other members ...
}

class Type_17
{
void action1() {}
int value = 8;

// other members ...
}

function foo(Type_1X x)
{
x.action1();
}
 
nwp
9:25 AM
@Milad I suggest you only put int value; in your base class and have the constructor of the derived class set the value.
 
@nwp If I don't put action1() in my base class how can I call it in foo()?!!!
 
nwp
Hmm. When I said "only put int value; in your base class" I meant "remove int value from your derived classes", not "remove void action1(); from your base class".
Also the base class needs to change to virtual void action1(); if you want to be able to call derived functions through the base.
 
@nwp got it.
@nwp there would be no real instance of the base class. The base just represents a set of other classes that have common members.
 
nwp
Though maybe instead of removing the int value = 3; you need to add Type_15() { value = 3; }.
And also it needs to be class Type_15 : public Type_1X { ... };.
@Milad That's what we're currently building. Though I'm not a fan of this type of inheritance shenanigans. It makes working with your classes difficult and inefficient.
 
@nwp got it. good suggestion.
@nwp what's the solution then?
@nwp I know. That was the problem code.
 
nwp
9:39 AM
@Milad It depends on the rest of the code. One solution is to not inherit from anything and use templates that assume that T::value and T::action1 exist and are of the right type.
Another solution is type erasure, but we probably don't want to go there.
Another solution is to merge all the classes into 1 class.
 
@nwp those can indeed work. I currently have a code that works. I just wanted to redesign it. My design is better than the alternatives. But haven't been able to do it in C++. In PHP, interface is used to do that.
 
nwp
Yeah, I know what you mean. In TypeScript you could easily define an interface for that as well. The closest thing C++ has to that is templates.
template <class Type_1X>
void use(Type_1X t) {
    std::cout << t.value;
    t.action1();
}
 
@nwp That's unfortunate.
 
nwp
And then you don't do any virtual or inheritance.
If someone passes in something that is not a Type_1X you get a somewhat unintelligible error message complaining about Sometype doesn't have a member value which is not very helpful. You can improve that with static_asserts that can test the given type and give a user defined error message like "Given type does not match interface Type_1X because it doesn't have an accessible .value member of type int" or similar.
But that takes a bit of effort to get right.
 
Templates don't look good for my good either. There is another way. That also looks ugly.
Just define the members of the base class with garbage. e.g., if it's a function that returns a value, just define a function that returns a dummy value (it won't be called and it will compile without error.
 
nwp
9:56 AM
That sounds worse, but it's difficult to tell without seeing the rest of the code. I recommend you go the template route.
 
@nwp do you think that C++ not having something similar to PHP interface is a language issue? Or do you think it's not even possible (considering the way C++ compiles and optimizes?)
 
nwp
Templates are the C++-equivalent to PHP interfaces. C++ could certainly have used better syntax. C++20 added concepts which improved on that front and are very close to interfaces.
If you look at the Hashable concept example it should be basically what you want.
I haven't used them yet though and it looks like MSVC only has partial support for them. If you use gcc 10 or clang 10 you can use them now.
"No requires expression support". As far as I can see you don't need that here, so you should be good.
Oh wait, I think you do. It starts with requires. Oops.
 
nwp
10:31 AM
@Milad What do you think of this? Also try hitting Edit and then change int value to double value and see what happens.
 
@nwp it looks nice. my code is quite complex. I'm trying to see if I can find a way to use template in it. will try your code afterward.
 
nwp
//interface
template<typename T>
concept Type_1X = requires(T t) {
    { t.value } -> std::same_as<int&>;
    { t.action1() };
};
That's probably better for the function. { &T::action1 } -> std::invocable<T>; doesn't let you overload action1.
 
nwp
Yeah, msvc doesn't support requires yet. At least as of version 19.23 (VS 16.3). Sucks.
template <class T>
void f(T t) {
    std::cout << t.value;
    t.action1();
}
That will work in msvc. You just don't specify the interface and instead define it implicitly through usage.
The error messages will be worse when you get it wrong, but it works.
 
@nwp what about comparison? I need operator == for Type_1X (e.g., Type_11 with Type_12, Type_12 with Type_15, etc.).
I just makes it all very complicated.
 
nwp
You can write a templated comparison operator, but you need to make some effort to not interfere with unrelated types.
It probably makes sense to write a is_Type_1X helper and reuse that.
 
11:20 AM
that's lots of work. with that dummy function method I could simply do:
bool Type_15::operator==(const Type_15& x) const
{
return (val == x.val);
}
bool Type_15::operator==(const Type_1X& x) const
{
return false;
}
 
nwp
Wait, that's not what == is supposed to do.
It's supposed to test equality based on having the same .value or something, not do a type check.
 
@nwp what do u mean?
@nwp That's the best solution that I've found.
 
are you implemting your own typeof() ?
 
nwp
bool operator==(Type_1X const auto &lhs, Type_1X const auto &rhs) {
    return lhs.value == rhs.value;
}
That's what I would do with concepts available, but it doesn't do the same as your operator.
 
@PeterT what do you mean?
 
11:27 AM
I was just making the same poinst as nwp that the comparison operator should probably not be used to do type-checking
 
@PeterT "should"? The code does not follow the standard guideline. I'm thinking about doing a lot worse than that.
 
Well "should", as in, people wouldn't expect it to. And while I quite enjoy a good surprise occasionally. "Suprising" is usually not a good attribute for code.
 
@PeterT I know! But trying to do the best.
 
I don't really know if the premise of "I need the derived classes to have a certain data member" is a great idea in C++. Why not just use a virtual getter function if it NEEDS to be OOP?
 
nwp
11:48 AM
Consider describing the actual problem you're trying to solve with your Type_1X classes. Maybe there is a better way.
 
@nwp I'm afraid it's complicated. But to make it short, each class is an instruction (like CPU instruction) that can be executed. e.g., class EQUALITY which tests to see if a given value is equal it's value, EMPTY (tests emptiness), etc.
 
nwp
I haven't dabbled in emulators, but having a class for instructions, especially with virtual functions, doesn't seem like a good approach.
 
12:04 PM
nope, no virtual function in the inner loop. That's going to kill performance
 
@nwp As a mater of fact, it does. It's complicated. I can explain but it's untoward.
 
it's honestly better to make your own jump-table like thingy. And if you absolutely need have some intermediate representation use something like a tagged union or so, where you don't need to rely on RTTI
 
@PeterT Yeah I guess
 
 
1 hour later…
1:37 PM
Update: I gave up. Didn't do it. Not worth it at the time.\
 

« first day (1694 days earlier)      last day (1161 days later) »