0

see these class:

class test
{
   public:
       void Created(){};
       test()
       {
           Created();
       }
};

now we can create objects from it. ok.
but can i overloading the scope-resolution ('::') for the object accept and change the Created() function?

4
Contributors
10
Replies
42
Views
3 Years
Discussion Span
Last Post by Ancient Dragon
Featured Replies
  • No you can not overload `::`. Why would you want to? Read More

  • You'd have to redeclare the function even if you could overload ::, so what's the problem? class test { public: virtual void Created() {cout << "Hello from test class\n";} test() { Created(); } }; class Child : public text { public: virtual void Created() {cout << "Hello from Child\n";} }; … Read More

  • >i understand that i can't use a varname outside the functions\class's with '::' operator Make the variable static and you can use it outside the class with :: opertor. class MyClass { public: static int x; }; int MyClass::x = 0; // static variables must also be declared like globals … Read More

  • Functions are coded as normal. class MyClass { public: static int x; static int foo(); }; int MyClass::x = 0; // static variables must also be declared like globals int MyClass::fo() { return x; } int main() { MyClass::x = 1; } Read More

0

for change the functions(with diferente objects) outside of functions\class and others.

(in my case) the problem of class direved from other class is that i must re-declare the functions

Edited by cambalinho

1

You'd have to redeclare the function even if you could overload ::, so what's the problem?

class test
{
   public:
       virtual void Created() {cout << "Hello from test class\n";}
       test()
       {
           Created();
       }
};

class Child : public text
{
public:

   virtual void Created() {cout << "Hello from Child\n";}
 };

 int main()
 {
     Child c;
     c.Created();  // call Created in Child class
     c.test::Created(); // call Created in test class
 }
0

some day ago i read something, so please correct me.
i understand that i can't use a varname outside the functions\class's with '::' operator, but seems that i can use with static functions, can you explain please?

1

i understand that i can't use a varname outside the functions\class's with '::' operator

Make the variable static and you can use it outside the class with :: opertor.

class MyClass
{
public:
   static int x;
 };

int MyClass::x = 0; // static variables must also be declared like globals

int main()
{
    MyClass::x = 1;
}
1

Functions are coded as normal.

class MyClass
{
public:
   static int x;
   static int foo();
 };

int MyClass::x = 0; // static variables must also be declared like globals
int MyClass::fo()
{
    return x;
}

int main()
{
    MyClass::x = 1;
}

Edited by Ancient Dragon

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.