Sins
Is there anyway to make program to chk prime number without using flag?
klimi
Ty S.
S.
😂
S.
Anybody helps me with the pure virtual function question ...
S.
#include <cstdio>
struct Base {
virtual void foo() = 0;
};
void Base::foo() {
puts("Base::foo");
}
struct Derive: Base {
virtual void foo() override;
};
void Derive::foo() {
this->Base::foo();
puts("Derive::foo");
}
int main() {
Base *base = new Derive{};
base->foo();
}
Hi guys. I just learnt the pure virtual function can be defined outside a class. But, is there any scenario for this usage?
S.
My guess is, it's just for alleviating the compiler's burden (since it's difficult to check
Zewdu
how can i write a program to check prime number between two number?
Anonymous
AppL
S.
...which means that's not recommended.
Anonymous
Thats correct
S.
Anonymous
Don't call virtual functions from a ctor or dtor
S.
So I don't see the point for the legitimacy/allowing of implementing a pure virtual function for the base class outside the base class.
S.
I just wonder why compiler allows this
Anonymous
S.
Yes. But why for a pure virtual function?
S.
My concern is for the pure virtual function. For "pure virtual functions", my understanding is that, "The base class doesn't provide an implementation. You must implement them in the derived class."
S.
But the example shows, the base class *can* provide an implementation for pure virtual functions, just outside the class.
Anonymous
S.
Yea
Anonymous
Maybe there are a few situations where it's usefull that you have the freedom to do that, but you're right, I also think it goes against the idea of pure virtual functions
S.
Anonymous
No problem :)
S.
Sorry but I don't see UB?
S.
Got it, thank you!
S.
BTW 10.3 Virtual functions [class.virtual]
A virtual function declared in a class shall be defined, or declared pure (10.4) in that class, or both; but no diagnostic is required (3.2).
... is from C++03. I'll check if it's changed in later standards.
S.
Anyway still I don't fully understand that sentence.
1. A virtual function declared in a class shall be defined,
2. or declared pure (10.4) in that class,
3. or both
are all ub?
S.
😂 Sorry for my poor English <- I'll carefully check the standards later
S.
Liam
S.
you wrote an ub.
10.3 Virtual functions
A virtual function declared in a class shall be defined, or declared pure (10.4) in that class, or both; but no diagnostic is required (3.2).
S.
1.4 Implementation compliance
The set of diagnosable rules consists of all syntactic and semantic rules in this International Standard except
for those rules containing an explicit notation that “no diagnostic is required” or which are described as
resulting in “undefined behavior.”
S.
because “no diagnostic is required” == ub?
Liam
because “no diagnostic is required” == ub?
No...
This is a rule for compilers. All syntactic and semantic rules in the C++ standard should be test and diagnose by compilers, but if a rule is marked as "no diagnostic is required" or "will result in undefined behaviour", then compilers is not required to test and diagnose it.
That is, for these rules, you can write in your code, and compiler will not report errors for them. However, linkers may, and the result of your program could be UB.
S.
got it now. thank you for the so detailed explanation!
Liam
My concern is for the pure virtual function. For "pure virtual functions", my understanding is that, "The base class doesn't provide an implementation. You must implement them in the derived class."
Pure virtual functions mark classes who contain them as "abstract class". It means that no objects of an abstract calss could be created, and we cannot use it as parameter types, as function return types, or as the type of an explicit conversion.
As a result, we'll never have a chance to call virtual member functions declared in an abstract class from its object. On the other hand, since it's a pure virtual function and will defintely be overrided in dirved classes. In consquence, in most cases, impl of pure virtual function is useless. Therefore, we can declare a function as pure function and give no definition of it.
But remember, there are two exceptions.
1. for pure virtual destructor, you'll have to provide a definition.
2. the member functions of the derived class are free to call the abstract base's pure virtual function using qualified function id (Base::some_pure_virtual_function()).
In these two cases, definition of pure virtual function makes sense.
S.
Pure virtual functions mark classes who contain them as "abstract class". It means that no objects of an abstract calss could be created, and we cannot use it as parameter types, as function return types, or as the type of an explicit conversion.
As a result, we'll never have a chance to call virtual member functions declared in an abstract class from its object. On the other hand, since it's a pure virtual function and will defintely be overrided in dirved classes. In consquence, in most cases, impl of pure virtual function is useless. Therefore, we can declare a function as pure function and give no definition of it.
But remember, there are two exceptions.
1. for pure virtual destructor, you'll have to provide a definition.
2. the member functions of the derived class are free to call the abstract base's pure virtual function using qualified function id (Base::some_pure_virtual_function()).
In these two cases, definition of pure virtual function makes sense.
Many thanks again! Completely understand now.
S.
( Maybe you can collect such discussions and publish them in your blog? could help more people
Liam
Sounds good.
Tony
Pure virtual functions mark classes who contain them as "abstract class". It means that no objects of an abstract calss could be created, and we cannot use it as parameter types, as function return types, or as the type of an explicit conversion.
As a result, we'll never have a chance to call virtual member functions declared in an abstract class from its object. On the other hand, since it's a pure virtual function and will defintely be overrided in dirved classes. In consquence, in most cases, impl of pure virtual function is useless. Therefore, we can declare a function as pure function and give no definition of it.
But remember, there are two exceptions.
1. for pure virtual destructor, you'll have to provide a definition.
2. the member functions of the derived class are free to call the abstract base's pure virtual function using qualified function id (Base::some_pure_virtual_function()).
In these two cases, definition of pure virtual function makes sense.
abstract class object type can not be used as parameter types but abstract class pointer type is ok.
Liam
S.
So references are ok too?
Tony
yes
Tony
the pointer type of object is binding in the execution period. you can think it is a unsigned int value that store the memory address of the object. when we call a virtual member function, it will search the virtual function map in the object memory. the virtual function map is constructed by the class construct function and destory by the destory function. It's the reason that we can't call virtual function in the construct function.
S.
Tony
yeah, you will get the answer in it. Good luck!!
Liam
Liam
considering that virtual call scheme is implementation defined...
Liam
Also, for gcc's vptr & vtbl method, all objects of a specific class share the same vtbl, which is pointed to by their own vptr.
Liam
see this for more details: https://liam.page/2018/01/23/crack-private-member-function-by-vtable/
sorry for other members in this chat group, this post is written in Chinese. and maybe I'll translate it into English in near future and then post on my en-blog.
Dima
o.o
Anonymous
To those of you who work as a developer: I had a little discussion with my co workers and they said "most developers don't program in ther free time cause they program enough at work". Do you guys work on own projects or contribute to OpenSource projects in your free time?
Liam
Tony
yes
Dima
I work on companys projects even at home if there’s some interesting thing, but mostly I am doing my own projects at home too
Dima
lets face that we don’t spend all time at work programming lol
Liam
well, you have this chat group now.
Liam
↑ to shelly
Anonymous
Yeah thats true^^
S.
😂 Not actually a developer though ... generally I write algorithms and my colleagues do the implementation. But now I just wanna know more language details and try to write C++ code
Tony
python?
S.
No ... just pseudo code
Anonymous
Thats cool :)
S.
But I have some knowledge on C 😄
Tony
@LiamHuang you're right.
MᏫᎻᎯᎷᎷᎬᎠ
MᏫᎻᎯᎷᎷᎬᎠ
The only difference between python and its psuedo code if exist is 1 line at most XD
Dima
> python
Dima
Liam
S.
😛
klimi
I'm Android developer
Tony
Any one is golang developer?
Parra