If that line has a problem(can cause memory leak?), can someone write the right way to write it?
class1 a = *(new class1());
The problem is that this line creates two objects, each of type class1. The first one is created with new and the pointer to it is lost (memory leak), the second one is created with the name a.
const int foo(int x,int y) {code...}
int z;
z=foo(a,b);
is z is now const and can't be changed after?
does z has to be declared as a const?
2.
what would be the result of:
void foo(int x, int y) const {code...}
what is const-safe?
3. virtual destructor.
What would happen if I wouldn't declare destructor of main class as virtual.
and called for destructor of one of the subclasses?
something like that:
1 2 3 4 5 6 7 8 9 10 11 12
class Customer{
protected:
string name
...
}
class RegularCustomer :public Customer
{
private:
int numberOfProducts;
}
and I would write:
1 2
customer a = new RegularCustomer();
delete a;
would I delete only the fields that are found in Customer class and not all the fields that added in RegularCustomer (I just gave a really basic example) ?
What would be the result it this code: constint foo(int x,int y) {code...}
there are no non-class const rvalues in C++. That const is ignored (some compilers give warnings:
gcc:
test.cc:1:26: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
const int foo(int x,int y) {return x+y;}
clang:
est.cc:1:1: warning: 'const' type qualifier on return type has no effect [-Wignored-qualifiers]
const int foo(int x,int y) {return x+y;}
intel:
test.cc(1): warning #858: type qualifier on return type is meaningless
const int foo(int x,int y) {return x+y;}
int z;
...
is z is now const
it can never be const; types do not change at runtime. int cannot become const int or vice versa.
what would be the result of:
void foo(int x, int y) const {code...}
this would fail to compile unless foo is a class member function
3. virtual destructor.
What would happen if I wouldn't declare destructor of main class as virtual.
and called for destructor of one of the subclasses?
The behavior of such program is undefined; anything at all can happen, there are no guarantees or expectations.