Please explain the scope resolution operator (::) & headers

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 2354 Views - Last Post: 15 July 2012 - 10:31 PM Rate Topic: -----

#1 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Please explain the scope resolution operator (::) & headers

Posted 09 July 2012 - 04:18 PM

I'm very new to C++, and I haven't really done any work that requires multiple files. I understand that the scope resolution operator specifies the namespace or class to which a function or member belongs, but coming from Java, I don't see why anyone would ever want to declare a function then code its functionality. It also seems very non-object oriented to me because it seems like it screws up encapsulation and stuff. Someone can just choose what a class's member functions do by saying "Class::function() { do something }" outside of the class. In other words, I don't see what stops someone from making multiple definitions for methods. And that leads me to wonder: are headers just completely redundant? Since they're not classes but merely skeletons of declarations, don't you have to copy the functionality of methods to each C++ file in which you include a header? I obviously don't understand headers and I'm hoping that all of my assumptions are wrong, but honestly - would it really be that hard to allow programmers to include regular C++ files that may or may not have classes in them for other code to use? I'm sure this is possible, but that makes headers one of the most useless files ever conceived.

I really appreciate your attempts to clear this up for me. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Please explain the scope resolution operator (::) & headers

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Please explain the scope resolution operator (::) & headers

Posted 09 July 2012 - 04:55 PM

Quote

I don't see why anyone would ever want to declare a function then code its functionality.
Is the following syntactically correct? How do you know?

int addFour(int a, int b, int c, int d)
{
    return addTwo(addTwo(a,B)/>,addTwo(c,d));
}



Quote

It also seems very non-object oriented to me because it seems like it screws up encapsulation and stuff.
Two things. 1) C++ doesn't force you to use an OO paradigm for your design. So, you if you talk about non OO code, well, yes, that's entirely possible.

2) OO doesn't imply good design. OO reflects a mental and design model, which means diddly squat if you don't have a problem that needs to be solved with that model. So talking about encapsulation is useless unless you can identify a clear design problem in your code that would be solved with OO.

Quote

Someone can just choose what a class's member functions do by saying "Class::function() { do something }" outside of the class.
Yes. That's called programming. You choose what the functions do.

Quote

In other words, I don't see what stops someone from making multiple definitions for methods.
That wouldn't be legal code. If you have multiple files defining the same function, you'll be forced to pick one.

Quote

Since they're not classes but merely skeletons of declarations, don't you have to copy the functionality of methods to each C++ file in which you include a header?
No, you don't. You don't need the definition to compile a file.

int addTwo(int, int);

int foo(int a, int B)/> { return addTwo(a,B)/>; }



That is perfectly compileable.

This post has been edited by Oler1s: 09 July 2012 - 04:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,010
  • Joined: 05-May 12

Re: Please explain the scope resolution operator (::) & headers

Posted 09 July 2012 - 10:11 PM

AVReidy, I sort of understand where you are coming from. I too questioned why double the work of declaring the methods and parameters in a header file, and then type it in all over again in the .cpp file. If you change the parameters of a method, you had to go edit it in two places.

As was explained to me, the use of headers and .cpp files was one of the early attempts to break class interface away from class implementation. In general, this is a good thing. People should write code against interfaces. The don't have to know the intimate details of the interface implementation. The theory was that the class definition in the header file would define the external facing interface of the class. The programmer was free to do all sorts of code revamps within the .cpp file, but for anybody including the header file, they got a nice stable interface to talk to. With stable header files, nobody had to keep recompiling their code. Remember that in the early days, a fast compile of a medium sized project was measured in tens of minutes, a slow compile was within the one-two hour range, and large projects too several hours to compile. People went to extremes to prevent recompiling the world if at all possible.

Unfortunately, the reality of things is such that often the code revamps within the .cpp file would require adding or removing member variables or methods and the header file would have to be touched anyway, causing a global compile. This is why a lot of old time C/C++ programmer practice very strict header include discipline, and they forward declared functions and classes whenever possible.

Some of this instability of class changes from the code revamps can be buffered by using the Pimpl design pattern, but I think this practice became popular too late. http://www.gamedev.n...e-c-pimpl-r1794

With Java and C#, there is no header file. The class declaration includes everything. With the fast processors we have nowadays, nobody thinks twice about refactoring the code and changing the way a class is written.

It's interesting to note though, that I think the redundant declarations are coming back, but this time C# and Java will be the prime culprits/victims. With the popularity of inversion of control and dependency injection techniques, now it's almost always required that one declare interface before declaring a class. So where as C#/Java code would traditionally just write:
class Foo
{
    public void Bar()
    {
        // do work here.
    }

    public string Fizz { get; private set; }

    private int Baz;
}


Now, if you use some IoC framework, you are almost always compelled to write if you know Foo will be passed around as a dependency:
interface IFoo
{
    void Bar();

    string Fizz { get; }
}

class Foo : IFoo
{
    public void Bar()
    {
        // do work here.
    }

    public string Fizz { get; private set; }

    private int Baz;
}


Was This Post Helpful? 1
  • +
  • -

#4 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Please explain the scope resolution operator (::) & headers

Posted 15 July 2012 - 05:17 PM

Also, is it normal practice to have only one .cpp file in a project? I don't see the point of having multiple since they're all stand-alone. It's not like I can include the functionality of a class, so I should just have 1 .cpp file that includes a million headers and creates every single method?
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Please explain the scope resolution operator (::) & headers

Posted 15 July 2012 - 05:22 PM

View PostAVReidy, on 16 July 2012 - 02:17 AM, said:

Also, is it normal practice to have only one .cpp file in a project?


No.

Quote

I don't see the point of having multiple since they're all stand-alone. It's not like I can include the functionality of a class, so I should just have 1 .cpp file that includes a million headers and creates every single method?


Note that header files are not supposed to contain function implementations (except for inline functions and templates). The normal practice is to define a class or declare a set of functions in the header file and then implement the functions in a cpp file with the same name. Then every cpp file that uses the class or functions, includes that header file.
Was This Post Helpful? 1
  • +
  • -

#6 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Please explain the scope resolution operator (::) & headers

Posted 15 July 2012 - 05:26 PM

But then you have to copy the methods or change them... that's just so unlike any other language I've worked with.

I still don't see how you can access a cpp file from another.
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Please explain the scope resolution operator (::) & headers

Posted 15 July 2012 - 05:31 PM

View PostAVReidy, on 16 July 2012 - 02:26 AM, said:

But then you have to copy the methods or change them...


What? No, you don't.
Was This Post Helpful? 0
  • +
  • -

#8 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Please explain the scope resolution operator (::) & headers

Posted 15 July 2012 - 05:40 PM

:helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Please explain the scope resolution operator (::) & headers

Posted 15 July 2012 - 05:52 PM

View PostAVReidy, on 16 July 2012 - 02:40 AM, said:

:helpsmilie:


I have no idea what that smiley is trying to tell me.
Was This Post Helpful? 0
  • +
  • -

#10 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Please explain the scope resolution operator (::) & headers

Posted 15 July 2012 - 05:58 PM

You can't access any functionality that isn't written in the running cpp file. If you could, cpp files could be included, and I would never have to worry about header files - it would work like something designed post-1965.

EDIT: I just included a cpp file, and it works, so I can program normally now. So what the heck are header files for? Surely the better practice is including cpp files with classes, much like importing in Java/Python?

This post has been edited by AVReidy: 15 July 2012 - 06:04 PM

Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Please explain the scope resolution operator (::) & headers

Posted 15 July 2012 - 06:02 PM

View PostAVReidy, on 16 July 2012 - 02:58 AM, said:

You can't access any functionality that isn't written in the running cpp file.


That statement is untrue. Just saying the same thing in different ways does not make it any more true.

If you've tried and it didn't work, then you either did not properly declare the function or you did not correctly link the object files. If you're using an IDE, you need to make sure that the cpp files that define the functions are in the same project as the ones that use those functions. If you aren't, you need to make sure that your Makefiles are set up correctly.
Was This Post Helpful? 0
  • +
  • -

#12 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Please explain the scope resolution operator (::) & headers

Posted 15 July 2012 - 06:09 PM

Well, I'm not using any makefiles. Didn't know that was part of compiling. But why do people use headers when you can write excellent object-oriented code by just including .cpp files with classes in them? I'm just so confused. Is my compiler a freak of nature by accepting an #include "Program.cpp"?
Was This Post Helpful? 0
  • +
  • -

#13 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Please explain the scope resolution operator (::) & headers

Posted 15 July 2012 - 06:14 PM

Quote

would it really be that hard to allow programmers to include regular C++ files

You can #include any files you want, and the compiler will copy/paste the contents where it was included. However, including files containing definitions (like cpp files) is asking for trouble, most commonly causing functions (et cetera) to be defined multiple times, causing ambiguity for the compiler.

Quote

EDIT: I just included a cpp file, and it works, so I can program normally now. So what the heck are header files for? Surely the better practice is including cpp files with classes, much like importing in Java/Python?

You will surely start running in to compiler errors, so expect them. Including source files is very rarely a good idea. The point is that the compiler should be the one to find the definitions, you shouldn't need to include source files.

Here's what it comes down to. Header files contain declarations. Source files contain definitions. Declarations tell the compiler that it should be able to find the definition at some point during compilation. However, you must tell the compiler about all the source files so it can find these definitions.

This post has been edited by Aphex19: 15 July 2012 - 06:17 PM

Was This Post Helpful? 2
  • +
  • -

#14 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Please explain the scope resolution operator (::) & headers

Posted 15 July 2012 - 06:19 PM

View PostAVReidy, on 16 July 2012 - 03:09 AM, said:

But why do people use headers when you can write excellent object-oriented code by just including .cpp files with classes in them?


Because you can't link together object files that contain a definition for the same function.

Quote

I'm just so confused. Is my compiler a freak of nature by accepting an #include "Program.cpp"?


No, the preprocessor doesn't care about the extension of the file you #include. However you will run into problems when you do this from multiple cpp files. I.e. if you have two cpp files that both #include Program.cpp and then you try to link those together, you'll get a linker error. Of course you could just #include every file into one main file and then compile that into one big object file, but that will lead to unacceptable compile times for larger projects.

Also you'd still need to forward-declare your functions if you don't want to define them in a specific order (or if they're mutually recursive) and once you do that, you've saved nothing compared to using header files.

Also you don't need to use Makefiles. It just makes working with everything but small projects easier.
Was This Post Helpful? 2
  • +
  • -

#15 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Please explain the scope resolution operator (::) & headers

Posted 15 July 2012 - 06:23 PM

Quote

Here's what it comes down to. Header files contain declarations. Source files contain definitions. Declarations tell the compiler that it should be able to find the definition at some point during compilation.


What you all are saying only makes sense to me if it's normal practice to include a header (almost) only inside of a .cpp file with the same name that contains method definitions. Meaning it must be normal practice to include the .cpp files when you want to use them.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2