Sign in to follow this  

Wierd Code 0.o

This topic is 2121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[quote name='Kalner' timestamp='1329782955' post='4915001']
So today I was getting into reading more on classes and I read on classes in seperate files and it was like Myclass::Myclass or something

so.. what does the "::" mean??
[/quote]
as unbelivever said its the scope resolution operator, read this: [url="http://msdn.microsoft.com/en-us/library/b451xz31%28v=vs.80%29.aspx"]http://msdn.microsof...v=vs.80%29.aspx[/url]

To make an example, lets say we have a class declaration (in a header file)
[code]
//MyClass.h
class MyClass {
private:
int someMemberVariable;
public:
MyClass(int v); //constructor
void doSomething();
int returnSomething();
static int staticHelperFunction(int x,int y);
}
[/code]
then we need an implementation for this class, we put that in MyClass.cpp

[code]
//MyClass.cpp

MyClass::MyClass(int v) { //The MyClass before the scope resolution operator is the name of the class or namespace the method exist in, the MyClass() after the :: is the constructor (constructors always have the same name as the class they belong to and no return type)
someMemberVariable=v;
}
void MyClass::doSomething() { //same thing here, this is an implementation of the doSomething() method in the class "MyClass"
someMemberVariable++;
}
int MyClass::returnSomething() { //i think you get it
return someMemberVariable
}
static int MyClass::staticHelperFunction(int x, int y) { //this is a static function and doesn't interact with the rest of the class, you'll see further down how we use it and how it relates to the :: operator
return x+y;
}
[/code]

to use this class we do

[code]
//programName.cpp
#include "MyClass.h"

int main() {
MyClass x;
x.doSomething();
int y = x.returnSomething();

//and now for the static function, as it is static we don't need to use the MyClass instance we created earlier (The one named x) but can simply do:
int z = MyClass::staticHelperFunction(5,9);

return 0;
}
[/code]

The example with the static method is a bad one in C++ as it is normally prefered to place helper functions in namespaces rather than in classes. (There are some exceptions where you need a helper function to for example call a private constructor in the class)

Share this post


Link to post
Share on other sites
Think of it like boxes within boxes, each box has a name attached to it, to access the box 'Bar' (using the above names) you need to first open the box 'Foo'
and in code you would do it like so:

Foo::Bar

Now say you wanted something contained within 'Bar' you would first have to go through Foo -> Bar -> Something, like so:

Foo::Bar::Something

If you don't know anything about scope you should take the time to read up on it, else you may struggle with it in the future.

Share this post


Link to post
Share on other sites

This topic is 2121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this