-5

I am reading a sample code that uses C++ and classes, I am new on C++ classes I can work with basics similar to this http://www.cplusplus.com/doc/tutorial/classes/, but I cant understand what the code below does mean or the color it is using visual studio c++

example

thanks

I am sorry if it is a fool question

Ron
  • 14,674
  • 4
  • 34
  • 47
Ale
  • 23
  • 7
  • 2
    And use a better learning resource. –  Oct 13 '17 at 16:54
  • can you recommend me one? – Ale Oct 13 '17 at 17:08
  • Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read a few introductory ones. – Ron Oct 13 '17 at 17:08
  • "if I am new programming in c++ there are not many websites with a trustworthy community to ask this kind of questions :/" I've not downvoted but equally I don't think you should ask those two questions at all. Syntax highlighting is using an editor 101, and the code has no reference or context. It is just a variable declaration with a superfluous keyword and a couple of meaningless function calls. You really should go for a guided education i.e. book or class and don't skip chapters, and don't worry if you don't instantly understand some segment of code, it will probably be explained :) – George Oct 13 '17 at 17:12
  • Thanks for the suggestion , I am a little confused here because I am coming from Event programming for microcontrollers to Object-Oriented, this is a big change for me – Ale Oct 13 '17 at 17:12
  • @Ale You can do event and object programming at the same time. Even on microcontrollers. Some MCU tool chains even fully support C++14. But still most MCU developers only want to use C. – Benjamin T Oct 13 '17 at 20:05

1 Answers1

0

It creates an object named some by instantiating the class some. Then it calls the member function ToVector() on the object some and pass the result of the call to the function named function.

  • class is blue because it is a keyword of the C++ language.
  • The first some is green because it is the name of a class.
  • The second some is black because it is a variable.
  • And function and ToVector are red because the are functions.

Now this is ugly code because you "hide" the class some by reusing the same name for your variable. Also you do not need to put the word class here.

Here is a more complete and nicer version:

#include <vector>

class Some
{
    public:
    std::vector<int> ToVector()
    {
        return std::vector<int>(); //return an empty vector
    }

};

int f(std::vector<int> v)
{
    return 0;
}



int main(int, char**)
{
    Some some; // Was "class some some"
    return f(some.ToVector());

}
Benjamin T
  • 8,120
  • 20
  • 37