C++11 auto className::functionName(Parameters..) -> Type


A few days ago I found a totally new syntax like
auto ClassName::functionName() -> something.

Then I started to find out what does it mean and got my answer. Today I am going to share this.
Lets we have a class as Person Class

what do you think? is there any problem?

Actually there is a problem. The enum Type declared inside the Class and when we will try to define the function outside the class, compiler will not found the Type there. In this case

Type Person::getType()
will cause an error.

Now there is a solution to avoid this error is to define the function inside the class. But what if you we don’t want to do that. How can we define such functions outside the class?
here comes the syntax We talked about.
auto className::functionName()->returnType

This is the feature of C++11.

in our case our function will be
auto Problem::getType() -> Type
{
return m_type;
}

Thank you.

About Razib Chandra Deb

I completed my BSc in CSE from CSE Department of KUET. Currently I am working as a Software Engineer in Samsung R&D Institute Bangladesh(SRBD). I am interested in image processing.
This entry was posted in C++ and tagged , , . Bookmark the permalink.

Leave a comment