about class's and '::' operator
see these code(i know that isn't correct):
Code:
class test
{
public:
void Printed(){};
void write(string a)
{
cout << a;
Printed();
}
};
test a;
void a::Printed()
{
cout << "\nmessage printed\n";
}
int main()
{
a.write("hello world");
return 0;
}
how can i change the Printed() for be valid with '::' with an object?
Re: about class's and '::' operator
Quote:
Originally Posted by
Cambalinho
how can i change the Printed() for be valid with '::' with an object?
You don't. '::' is the scope resolution operator. You can only use it after a namespace or class/struct name to specify the scope in which the subsequent identifier should be looked up.
Printed() is a member function of test. To implement that outside the class definition, you need to specify that you mean the Printed member function of test, rather than a global function called Printed. You do that by writing test::Printed.
Re: about class's and '::' operator
Quote:
Originally Posted by
D_Drmmr
You don't. '::' is the scope resolution operator. You can only use it after a namespace or class/struct name to specify the scope in which the subsequent identifier should be looked up.
Printed() is a member function of test. To implement that outside the class definition, you need to specify that you mean the Printed member function of test, rather than a global function called Printed. You do that by writing test::Printed.
i understand what you mean, but i need 1 way for change the function with object name and outside of main(instead use '::', i can use the dot... but try understand what i mean(sorry my english)
Re: about class's and '::' operator
Quote:
Originally Posted by
Cambalinho
i understand what you mean, but i need 1 way for change the function with object name and outside of main(instead use '::', i can use the dot... but try understand what i mean(sorry my english)
Maybe you should try to describe your problem, rather than describing which solution you think will work.
At a higher level, what do you want to do?
Re: about class's and '::' operator
Quote:
Originally Posted by
D_Drmmr
Maybe you should try to describe your problem, rather than describing which solution you think will work.
At a higher level, what do you want to do?
do you know VB code? so i need create objects that can change the functions(events) outside of main.
sorry, i understand the C++ isn't realy prepared for these.... but i need do something like these. i can use the directive, but i must re-declare the virtual functions.
Re: about class's and '::' operator
A class function is specified to be part of a class. Every instance of that class has the same set of functions. A class instance cannot have a different function from another class instance. Even with polymorphism and virual functions, all of the instances of the same derived class have the same functions.
From your example, you can do something like this
Code:
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
class test
{
public:
void Printed();
void write(string a)
{
cout << a;
Printed();
}
void SetText(const string& txt) {
text = txt;
}
private:
string text;
};
void test::Printed()
{
cout << text;
}
int main()
{
test a;
a.SetText("\nMessage printed\n");
a.write("hello world");
return 0;
}
where for each instance of the class you can specify some data.
Re: about class's and '::' operator
the Printed() is a member function of the class named 'test'
so the function definition needs to be test::Printed()
Re: about class's and '::' operator
Quote:
Originally Posted by
Cambalinho
do you know VB code? so i need create objects that can change the functions(events) outside of main.
sorry, i understand the C++ isn't realy prepared for these.... but i need do something like these. i can use the directive, but i must re-declare the virtual functions.
That doesn't really make sense. All C++ programs start with main, which in turn calls other functions. "Objects that can change the functions" also doesn't make sense. What do you mean "change the functions"?
You may be talking about static functions, which are members of a class that can be called without an instance of the class, but it's really hard to tell what you're trying to do.
Re: about class's and '::' operator
Quote:
Originally Posted by
Cambalinho
do you know VB code? so i need create objects that can change the functions(events) outside of main.
sorry, i understand the C++ isn't realy prepared for these.... but i need do something like these. i can use the directive, but i must re-declare the virtual functions.
What you describe is how you would solve your problem in VB (at least, I assume; I don't really understand what you want to do). That's not useful for solving a problem in C++, because C++ is not at all like VB. If you explain your problem - not your solution - then we can help you find a solution in C++.
Re: about class's and '::' operator
Quote:
Originally Posted by
GCDEF
That doesn't really make sense. All C++ programs start with main, which in turn calls other functions. "Objects that can change the functions" also doesn't make sense. What do you mean "change the functions"?
You may be talking about static functions, which are members of a class that can be called without an instance of the class, but it's really hard to tell what you're trying to do.
I think he's trying to describe/implement instance based polymorphism as opposed to class based polymorphism - but that isn't possible in the c++ language as far as I am aware.
Re: about class's and '::' operator
Quote:
Originally Posted by
2kaud
I think he's trying to describe/implement instance based polymorphism as opposed to class based polymorphism - but that isn't possible in c++ as far as I am aware.
thanks for help me with the right terms ;)
Re: about class's and '::' operator
the simplest way to implement "per instance member functions" is to use std::function ; eg, your code snippet would become
Code:
#include <iostream>
#include <string>
#include <functional>
using namespace std;
class test
{
public:
std::function<void()> Printed;
void write(string const& a)
{
cout << a;
Printed();
}
};
test a = { []
{
cout << "\nmessage printed\n";
} };
int main()
{
a.write("hello world");
return 0;
}
if your compiler is not c++11-ready, you can use boost::function instead.
Anyway, note that there are gazillions ways of binding runtime behaviour to a class instance, depending on your requirements. So, as others said, you'd better explaining what you're trying to do at a higher level, and no, writing code this way because "VB code does so" is not a good idea ...
Re: about class's and '::' operator
Quote:
Originally Posted by
superbonzo
the simplest way to implement "per instance member functions" is to use std::function ; eg, your code snippet would become
Code:
#include <iostream>
#include <string>
#include <functional>
using namespace std;
class test
{
public:
std::function<void()> Printed;
void write(string const& a)
{
cout << a;
Printed();
}
};
test a = { []
{
cout << "\nmessage printed\n";
} };
int main()
{
a.write("hello world");
return 0;
}
if your compiler is not c++11-ready, you can use boost::function instead.
Anyway, note that there are gazillions ways of binding runtime behaviour to a class instance, depending on your requirements. So, as others said, you'd better explaining what you're trying to do at a higher level, and no, writing code this way because "VB code does so" is not a good idea ...
what you did was initializate with constructor. but, i belive, theres another way, but i don't know what is :(
i'm sorry but the '()' is used out of scope(too), so why i can't overload it for use it in these way?
Re: about class's and '::' operator
You can do 'instance based polymorphism' (is that even a standardized terminology) ?
by using lambda's
or by using function pointers
or functors/ function objects
or one of many other possible solutions.
it just requires "a bit" of explicit code assistance. I.m.o. this is just "implementation, which made me confused when I read that "instance base polymorphism" as being a "real" thing.