-
Sep 6th, 2014, 05:30 PM
#1
Thread Starter
PowerPoster
c++ 11 - can i change functions in Global Scope section?
see these code:
Code:
#include <iostream>
#include <string>
using namespace std;
class test
{
public:
virtual void ola()
{
cout <<"hi" << endl;
}
test()
{
//nothing;
}
};
class test1 : public test
{
public:
test1()
{
//nothing;
}
};
void test1::ola()
{
cout << "hello" << endl;
}
int main()
{
//cout << test::a << endl;
return 0;
}
i get 1 error the 'ola' isn't 'test1' member. so the virtual functions must be re-declared
is there anyway for resolve these for avoid re-declare the functions?
my objective is change their values in Global Scope
-
Sep 8th, 2014, 07:06 AM
#2
Re: c++ 11 - can i change functions in Global Scope section?
Code:
oid test1::ola()
{
cout << "hello" << endl;
}
as you are defining the definition of the function ola() for class test1 separately from the declaration of the class test1 then the class test1 needs to have the function ola() declared.
Code:
#include <iostream>
#include <string>
using namespace std;
class test
{
public:
virtual void ola();
test()
{
//nothing;
}
};
class test1 : public test
{
public:
virtual void ola();
test1()
{
//nothing;
}
};
void test::ola()
{
cout << "hello from ola test" << endl;
}
void test1::ola()
{
cout << "hello from ola test1" << endl;
}
void virt(test *cs)
{
cs->ola();
}
int main()
{
test *o = new test;
test *o1 = new test1;
virt(o);
virt(o1);
return 0;
}
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Sep 8th, 2014, 07:39 AM
#3
Thread Starter
PowerPoster
Re: c++ 11 - can i change functions in Global Scope section?
Originally Posted by 2kaud
Code:
oid test1::ola()
{
cout << "hello" << endl;
}
as you are defining the definition of the function ola() for class test1 separately from the declaration of the class test1 then the class test1 needs to have the function ola() declared.
Code:
#include <iostream>
#include <string>
using namespace std;
class test
{
public:
virtual void ola();
test()
{
//nothing;
}
};
class test1 : public test
{
public:
virtual void ola();
test1()
{
//nothing;
}
};
void test::ola()
{
cout << "hello from ola test" << endl;
}
void test1::ola()
{
cout << "hello from ola test1" << endl;
}
void virt(test *cs)
{
cs->ola();
}
int main()
{
test *o = new test;
test *o1 = new test1;
virt(o);
virt(o1);
return 0;
}
i'm sorry, but that solution don't resolve my problem
i have read something, but i can't find the sample: the 'override' can resolve my problem?
do you ever used VB6? i'm trying doing procedures\functions like the 'events'. be changed in Global Scope by instances names. but the C++ don't let me use intances in Global Scope... only when i declare them and inicialize them.
-
Sep 8th, 2014, 12:36 PM
#4
Re: c++ 11 - can i change functions in Global Scope section?
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Oct 5th, 2014, 01:55 PM
#5
Re: c++ 11 - can i change functions in Global Scope section?
Originally Posted by 2kaud
Code:
oid test1::ola()
{
cout << "hello" << endl;
}
as you are defining the definition of the function ola() for class test1 separately from the declaration of the class test1 then the class test1 needs to have the function ola() declared.
Code:
#include <iostream>
#include <string>
using namespace std;
class test
{
public:
virtual void ola();
test()
{
//nothing;
}
};
class test1 : public test
{
public:
virtual void ola();
test1()
{
//nothing;
}
};
void test::ola()
{
cout << "hello from ola test" << endl;
}
void test1::ola()
{
cout << "hello from ola test1" << endl;
}
void virt(test *cs)
{
cs->ola();
}
int main()
{
test *o = new test;
test *o1 = new test1;
virt(o);
virt(o1);
return 0;
}
You dynamically allocated those objects but never delete them which is very bad. Also, why is the class that inherits test publicly have a virtual keyword on ola()?
I would also not suggest mixing around prototypes and declarations like this. However, here is the solution you want:
Code:
#include <iostream>
using namespace std;
class test
{
public:
virtual void ola();
};
class test1 : public test
{
public:
void ola();
};
void test::ola()
{
cout << "hello from ola test" << endl;
}
void test1::ola()
{
cout << "hello from ola test1" << endl;
}
int main()
{
test t;
t.ola();
test1 t1;
t1.ola();
}
I was also confused on why you included <string>, so I have removed that as well.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|