kotoko
kotoko

Reputation: 599

Understanding c++ block of code

I was given a c++ main and have to code it so it works.

I am having some trouble understanding the code as I am a bit new to cpp.

Here is the code

int main(int argc, char *argv[]) {
  Class::setAtribute("string"); 
  Class(Class::CONSTANT) << "starting up..."; 
}

Some questions:

  1. How can the first line work with no variables? Is it static?

  2. The second line is really strange for me, what I can make out is a Constructor that takes in a class constante and then prints it out somehow?

    If someone could explain me this bit of code it would be great!

    Thanks in advance.

Upvotes: 0

Views: 146

Answers (3)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136256

How can the first line work with no variables? Is it static?

Class::setAtribute() must be a static function in class Class. A static function doesn't need an instance of a class (object).

The second line is really strange for me, what I can make out is a Constructor that takes in a class constante and then prints it out somehow?

Right, it constructs an instance of Class passing Class::CONSTANT as the argument to Class constructor. For Class(Class::CONSTANT) << "starting up..."; to compile there must be an overloaded operator<< in the form of:

As a member function of Class (David Rodríguez - dribeas):

<some_return_value_type> Class::operator<<(char const*);

or as a free-standing function:

<some_return_value_type> operator<<(Class const&, char const*);

or:

<some_return_value_type> operator<<(Class const&, std::string const&);

or, in C++11:

<some_return_value_type> operator<<(Class&&, char const*);

The second argument, in fact, can be anything that can be constructed from a string literal char const[]. Or, alternatively, Class can have a conversion operator to, say, std::ostream&, so that std::ostream& std::operator<<(std::ostream&, char const*) is picked instead. Looking at Class definition and free-standing functions in its namespace must yield a definite answer.

Upvotes: 2

How can the first line work with no variables? Is it static?

Most probably, you should go and check the headers for the definition of Class and verify, but I am 90% sure it is a static method call.

The second line is really strange for me, what I can make out is a Constructor that takes in a class constante and then prints it out somehow?

You kind of have it. It most probably (again check the sources) creates a temporary object of type Class initialized with Class::CONSTANT (there is a constructor that matches the type of the static member Class:CONSTANT). Then operator<< is called on that temporary. In this particular case, operator<< is most probably implemented as a member function of Class that takes either a const char* as argument or something convertible from it, like a std::string.

Upvotes: 0

Wyzard
Wyzard

Reputation: 34563

the first line is declaring the main function, which is where the program starts running. If you're used to Java or C#, yes, it's effectively static; it's not associated with an instance of any class. Remember that in C++, you can have standalone functions that aren't even part of a class. main is one of those.

The second line is calling a static method called setAtribute on a class called Class, and passing it a string argument. It's not clear what the purpose of that line is, but as far as how it works, it's just an ordinary function call, not a constructor.

Edit: I just realized that when you said "first line", you might have actually meant the line I called the "second" one. In that case you're asking about the third line too. Yes, that's constructing a temporary instance of the class, then calling its << operator (presumably to print something).

Upvotes: 0

Related Questions