Reputation: 13575
I have a cpp file which only uses only one method of a large class. To do that I usually include the large class declaration header file. Can I just include a particular member method of a class?
Upvotes: 1
Views: 53
Reputation: 15870
Short answer: No.
When you include a header, you include the whole header (minus anything that may be stripped out via preprocessor directives).
Additionally, if the header you are including declares a "large class", it is highly likely that class should be refactored.
Finally, it won't matter in the end as the compiler will optimize things for you. Do not try to optimize things until you have profiled them (and in this case, even if you could do this, it wouldn't be of any benefit).
Upvotes: 1
Reputation: 4939
No you cannot include just one method from a class by a #include
preprocessor command. For the most common cases the compiler will need to know the class and all of the methods to know that the method you are using exists.
Now in dealing with template classes, the compiler will only compile the code/functions which are used, but you still must include the entire class so that the compiler can check if the method exists. For example:
template<class A>
class A {
public:
void foo();
void bar();
}
int main {
A a;
a.foo();
}
In the previous case, only foo is compiled and optimized, not bar.
Upvotes: 0