MᏫᎻᎯᎷᎷᎬᎠ
Like dividing by zero
BinaryByter
there's stack unwinding
BinaryByter
etc
MᏫᎻᎯᎷᎷᎬᎠ
BinaryByter
the reason why exceptions are better is that they are clearer
MᏫᎻᎯᎷᎷᎬᎠ
Exception provide a better mechanism to handle the error
MᏫᎻᎯᎷᎷᎬᎠ
which one?
Try catch
MᏫᎻᎯᎷᎷᎬᎠ
Try catch
Because they are clear
BinaryByter
thats the reason why exceptions are better
MᏫᎻᎯᎷᎷᎬᎠ
You don't wanna fill your program with if else
BinaryByter
but (IMHO) the reason why array is better than c-style arrays is that they have iterators, strong typing, move semantics, and they have some dank utilities
BinaryByter
You don't wanna fill your program with if else
Not relevant to the question "why are exceptions better than sigsevs?"
MᏫᎻᎯᎷᎷᎬᎠ
But if you wanna have a variable lenght (not resizable) you better use the style array
BinaryByter
which array?
BinaryByter
the c style array or the C++ style array?
BinaryByter
Yup
the c style array xor the c++ style array?
MᏫᎻᎯᎷᎷᎬᎠ
You can't do this int a = 8; std::array<int, a> x;
BinaryByter
with constexpr you can
MᏫᎻᎯᎷᎷᎬᎠ
with constexpr you can
You cant with const
BinaryByter
but you cannot do that with C style arrays as well, btw
MᏫᎻᎯᎷᎷᎬᎠ
BinaryByter
but you cannot do that with C style arrays as well, btw
int a; int b[a]; is valid C11 but invalid C++
MᏫᎻᎯᎷᎷᎬᎠ
BinaryByter
(in C++, we use a cheap workaround where we declare an int b[1];)
BinaryByter
Yes it is🤔
so your point is kinda invalid
MᏫᎻᎯᎷᎷᎬᎠ
Hmmm
Magnvm_Khaos
You answered yourself
And if i want to insert contacts everytime i want?
Raul
Frens, another question, that I am 100% sure I know the answer to. is there a way to have a template that can take any arbitrary class? For example, say I have: Class1 a; Class2 b; Class3 c; And I want to be able to have a driver: BaseClass base; But I want to be able to pass in Class1, and Class2 into the constructor of the base class without having to specifically state: BaseClass(Class1 A, Class2 B); I would love to do: template <class A, class B> BaseClass { BaseClass(A &class1, B &class2); }; However, the problem with this is I won't know what class1 or class2, or have any access to any of their functions. Is there a way I can do this? Or do I have to specifically state in the constructor a specific class?
Raul
Sorry that it is so long!
Mat
And if i want to insert contacts everytime i want?
You use a big enough array. Or you just read the other answers
olli
but (IMHO) the reason why array is better than c-style arrays is that they have iterators, strong typing, move semantics, and they have some dank utilities
- std:;array offers value semantics instead of decaying to a pointer, so you can easy return or copy it. - std::array does support Random Access Iterators. (C style arrays support Bidirectional Iterators at least) But I doubt std::array would benefit from move semantics
Raul
However, now I am trying to figure out how to combine any_cast with this.
olli
Actually, somehow it was working the entire time. Templates are magic.
If the types you pass define the funcitons you call, there is no issue. Try passing something like int or void* and chances increase it will fail
Raul
If the types you pass define the funcitons you call, there is no issue. Try passing something like int or void* and chances increase it will fail
so what if I do this: map.emplace("setValue",&A::setValue); What is my any_cast going to look like? I tried: std::any_cast<void(*)(int)>(mapIter->second)(newVal); This compiles, but it throws a bad_any_cast exception. So what should I be casting it to? I'm assuming void(*) is incorrect here.
Raul
because the type is not the same. You take a member function, which is probably of type void(A::*)(int)
that is what I did haha, problem now is now I get the compiler error that I must use .* or ->* to call pointer-to-member function. No idea what to do with that now. And I am not sure why I need to do that because it is my map that belongs to the class.
Raul
What are you actually trying to do? Pretty sure that is not a good solution this works though https://godbolt.org/z/NbF9Ws
Honestly, you are correct. This is a bad solution what I am trying to do is this: template<class A, class B> class BaseClass { public: BaseClase(const A &class1, const B &class2) { bindThem(); } void setValue(int newVal) { auto mapIter = map.find("setVal"); std::any_cast<void(A::*)(int)>(mapIter->second)(newVal); } void bindThem() { map.emplace("setVal",&A::setValue); } private: std::unordered_map<std::string,std::any> map; }
Raul
the EASIER, and best way, would be to create two private class variables and then call: classA.setValue(newVal); classB.setValue(newVal);
Raul
I am just trying to do something else for fun and making it harder on myself because that's how we learn about this stuff.
Anonymous
Hi guys
www.solutionsadda.in
Hi friends
olli
Honestly, you are correct. This is a bad solution what I am trying to do is this: template<class A, class B> class BaseClass { public: BaseClase(const A &class1, const B &class2) { bindThem(); } void setValue(int newVal) { auto mapIter = map.find("setVal"); std::any_cast<void(A::*)(int)>(mapIter->second)(newVal); } void bindThem() { map.emplace("setVal",&A::setValue); } private: std::unordered_map<std::string,std::any> map; }
Still, why would you want to do this? Your setValue does not work, you need an object to call the member function on. What problem are you trying to solve that this does not solve? template <class A> class Base { A & a; public: Base(A & a) : a(a) {} void set(int x) { a.setA(x); } }; You can use it like this A a; Base<A> ba{a}; ba.set(3);
Aurora
.
Aurora
what does void (*)(int);and A::* mean?
BinaryByter
function pointers
Yiii
hello guys? my name is fit. i need some help from you guys.. i have a project for my subject and its c++ language.. about insert how many information and then calculate it. the problem im having right now is on the picture as u can see the spacing is not right cause i dont know how to manage it. i use while to call for the output. ========================= does any of u guys know how to let the output stay on the same line eventhough the information inserted too short or too long? thanks. sorry for bad english and no0b coding cause i only learn the basic.
Yiii
here is some of the code about the output..
Raul
Still, why would you want to do this? Your setValue does not work, you need an object to call the member function on. What problem are you trying to solve that this does not solve? template <class A> class Base { A & a; public: Base(A & a) : a(a) {} void set(int x) { a.setA(x); } }; You can use it like this A a; Base<A> ba{a}; ba.set(3);
I am trying to do this because I am trying to save myself from doing class1.set(5) class2.set(5) class3.set(5) Whenever something in a UI happens. It would be much cleaner to have to just have a base class that acts as driver and modifies the code if they depend on each other. Say one class is an OpenGL setter function, one class is a textBox that waits for input, and another is a slider that adjusts color. And that slider affects the openGL call and the input of the text box. And maybe I have more than one slider. See the issue now? Everytime I have to make an update I have to update those things within a code manually. This can get ugly and complex really fast. So I am trying to simplify things into just one base that can handle all of this when an update happens. This class will only know of the getter and setter functions. That's it. So I am trying to wrap them into a class without having to do: update (int value) { ui->mySlider->setValue(value); ui->myTextBox->setValue(value); GLClass->setValue(value); ... // More objects to set } Instead it would be nice to just modify what is necessary. update(ui->mySlider, GLClass,value) { BaseClass<ui->mySlider,GLClass> base; base->setValue(value); }
MᏫᎻᎯᎷᎷᎬᎠ
Hello
@linuxer4fun Is that you first message?!
BinaryByter
dunno
BinaryByter
its well possible
Anonymous
thank you all
Anonymous
hi
Aurora
Anonymous
hi I have a variable (x), and I want to store several data in one variable. How can I do this in C language?
BinaryByter
you can't
BinaryByter
(you cannot easily atleast)
BinaryByter
thats not the same
BinaryByter
no
Anonymous
Order in one variable For example a variable variable named ... x = 33; ... x = 66; ... x = 90; But can data be saved and can be called and stored
BinaryByter
those are separate variables next to eachother
Anonymous
MᏫᎻᎯᎷᎷᎬᎠ
those are separate variables next to eachother
int *p; p++; content 1 p++; content 2 . . And so on
BinaryByter
thats not one variable
MᏫᎻᎯᎷᎷᎬᎠ
One variable
BinaryByter
those are multiple variables
BinaryByter
but one pointer
MᏫᎻᎯᎷᎷᎬᎠ
thats not one variable
Define the variable
Anonymous
How can I not do it?