C++ unique ability

1. inline function: compiler may expand inline function just like marco to improve performance.

inline int pow2(int);
inline int pow2(int num)
    return num*num;

2. default parameter value:

double area(double, double = 3.14);
Double area(double r, double pi){ return r*r*pi; }

3. return reference: doesn’t know when to use this.

string& foo();
String& foo()
    String *s = new string("this is a string");
    return *s

4. variable-length argument: just the same as C, but defined inside #include<cstdarg>

5. auto pointer: instance which created by keyword ‘new’ won’t release even though function ends. You need to call delete(). With auto pointer, garbage collection will be enable.

Auto_ptr<int>iPtr (new int(100));

6. new and delete: support primitive type.

Int* iptr = new int;
In* iptrArr = new int[100];
Delete [] iptrArr;

7. mutable: if you want a property of a constant instance to be changable, use keyword ‘mutable’

    Mutable int someInt;

8. keyword ‘friend’: let an external function to access a private variable of a class, to improve performance.Class Foo

Class Foo
      Friend void bar(foo);
      Int I;
Void bar(foo f)
    Cout << f.i;

9. virtual inheritance: to solve diamond problem, root class A, subclasses B1,B2 of A, subclass C of B1,B2. prevent C to have  two copy of A

Class A {};
Class B1 : virtual publicA {};
Class B2 : virtual publicA{};
Class C : public B1, public B2{};

10.defining type casting: can be primitive type and your own class

Operator int() { ... return someInt;}
Class someClass{
   Operator OtherClass(){... return otherClass;}

One more thing, it seems to me that C++ allow function which doesn’t belongs to any class, just like C, but I think this is not allowed by C# and JAVA. Correct me if I am wrong.


3 thoughts on “C++ unique ability

  1. 3. It certainly compiles but you should avoid returning references for objects when the programmer that calls this function has to manage the object’s life-time. The signature string& func1() doesn’t say who’s responsible for the string object whereas auto_ptr func2() is rather self-explaining with respect to the string object’s life-time question

    4. You should avoid this. It’s nothing but trouble 🙂

    11. Yes, C++ supports “free functions” just like C. Free functions taking parameters of some class type defined in the same namespace are also considered part of the interface of the class. Google for “interface principle”.

  2. Thanks Pizer. I am unaware how free function works.
    BTW, I read you blog today and those articles are great. Though the topics are a little difficult for me, C++0x, DSP, etc.
    I just want to say, great blog, keep it up.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s