...menustart
- Reference
- friend function
- difference between plain Enum and Enum class in C++ ?
- How to prevent someone from taking address of your object?
- How to prevent someone from inheriting from your class ?
- How to return mulitple values ?
- How to assign object to int ?
- Why use
override
keyword ? - Why copy constructor take argument as reference ?
- Function Hiding in C++
...menuend
- alias , same memory address
- Reassignment is NOT possible with reference
- can NOT make a reference with value NULL
- Use reference in function parameter and return type(best use for reference)
-
void fun(BigClass& obj) { obj->_data ; } int main() { BigClass obj; fun(obj); return 0; }
-
void change(int & x) { x = 10 ; } int main() { int x = 5 ; change(x); return 0; }
- To achieve run-time polymorphism in a function
-
- Use pointers in algorithms and data structures like linked list, tree, graph etc...
- Why?
- sometime we put NULL/nullptr in node
- sometime we change pointers to point some another node
- Why?
- mainly used for giving rights explicitly
- i.e. used for testing
- friend function don't have the permission to access the parent Class.
- you invited a friend , he can enter into your room, but not your parents' room.
// plain Enum
enum Color1{red, green, blue};
Color1 c1 = red ;
// Class Enum
enum class Color2{red, green, blue};
Color2 c2 = Color2::red ;
-
class Base { int x; public: Base() {} Base(int x):x{x} {} private: Base* operator & () { return this ; } } ;
-
class Base { int x ; public: Base () {} Base (int x):x{x} {} Base* operator & () = delete ; } ;
- just use
final
keyword
class Base final {
...
}
- use some struct/class and fill the values in that.
- use std::tuple in c++11
class Base {
...
public:
operator int() const {
return var;
}
}
class Base {
...
public:
virtual void fun() {
...
}
}
class Derived: public Base {
...
public:
void fun() override {
...
}
}
- Testing become easy with this ... ( easy maintainance )
- Compile-time check can be performed. ( future error could be reduced )
void fun(int a)
will pass compilation, whilevoid fun(int a) override
failed.
Foo(const Foo obj) {
...
}
A: To avoid infinite recursive.
- in a class, function with same name and different parameters will be overloaded.
- in a base class and a derived class, same name function will be hidden from derived class.