inline functions in templates

Hi

Can i call class A's functions from class B's header if i include class A header in class B's header?

eg:
//file B.h

#include A.h
template <class T=int>
class B{

  //inline
   void myfuncA(){ A.f();}
};

There should be nothing conceptually wrong in doing that? Specifically if i am interested in turning B to a template class.
LuckyLucksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
>> Can i call class A's functions from class B's header if i include class A header in class B's header?
Yes, there is nothing wrong with doing that and, in fact, if class B becomes templated you have little choice. Just be careful about including headers in headers and only do it where really necessary otherwise you end up with longer compile times and exposing symbols where they may not be needed, which can lead to ambiguity issues and namespace pollution.

>> There should be nothing conceptually wrong in doing that? Specifically if i am interested in turning B to a template class.
No, conceptually this is fine, but with all the caveats I allude to above.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
void myfuncA(){ A.f();}

this code will not work since when using A. or A-> the A must be a variable and not a class (name).

but if your class name is A you can't use that name for a variable.

So you would need to change your code like

void myfuncA()
{ 
   A a;   // define a variable of type A
   a.f();  // call member function f
}

Open in new window


if function f is a static member function you may call it without using an instance (object) of class A. then you could have member functions of defined inline like

#include "a.h"
class B
{
      ...
     void myfuncA(){ A::f();}

Open in new window


when f is public static member function of class A.

Sara
0
sarabandeCommented:
there is conflicting answers between sara and evilrix.
actually, there is no conflict.

the question asked by LuckyLucks was ambiguous since the code sample given by the questioner to describe the issue was wrong syntax and did not fit to the question respectively. because of that i suggested that the questioner has made some wrong assumptions.

while evilrix answered the question - not commenting on issues of the code - i took the other route and corrected the code. that way i gave a solution to the question (since the valid code showed how to call a function from class A in a member function of class B) without repeating the answer evilrix has given.

note, if there really had been a conflict between answers i would have expected to discuss that in the thread. that would have been constructive both for me and the author and surely had lead to a mutual conclusion. the current procedure though seems to indicate that the author has made a mistake which needs to be corrected. if that impression is right you should give a plausible reasoning at first.

Sara
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

evilrixSenior Software Engineer (Avast)Commented:
I answered the question asked (correctly). You observed a syntax issue in the example. It should have been a split. The question did not ask us to observe any syntax issues in the example, which are often quickly throw together (and are often syntactically incorrect pseuo-like code)  to demonstrate the point being made.

Note, the very first line in the example says,  "eg" - short for example! Also note the code is clearly incomplete since everything in class B is private and so even if the syntax issue is fix the code as it stands is good for nothing than a representative example.
0
sarabandeCommented:
I answered the question asked (correctly).
actually the title of the question 'inline functions in templates' shows that neither you and me gave a full answer to the complex. obviously the author wanted to know whether template classes with inline code can call member functions of other (non-template) classes as well. you said 'if class B becomes templated you have little choice.'  what on the best indirectly answered this part of the question.

You observed a syntax issue in the example.
yes. i was second in answering the question. hence i needed to add information or have my own viewpoint on what the real problem was. the expression A.f() used in the sample code showed that the asker did not know the difference of member functions and static member functions and was not aware that non-static member functions only could be called with an object of the class.

the very first line in the example says,  "eg" - short for example!
from my experience examples show up the issues the questioner is fighting against. they are more reliable than the wording of question itself since the wording highly is dependent on the knowledge the questioner already has.

we don't need agree on this if we simply ackknowledge that both positions are valid for answering questions here at ee. if that is ok, none of us must pardon to have answered as we did and there is nothing conflicting with the answers themselves, right?

It should have been a split.
you know that i didn't have objected on a split nor if your answer would have got all points.

however, if my suggestion is right and the question was not-quite-correctly asked, then it was my answer which has been a solution for the questioner and it is quite understandable why my answer was accepted as the only solution.

but anyway, i am more interested in clearing up misunderstandings and wrong manner of speaking rather than in getting points.

so i would welcome if LuckyLucks makes a decision again and would like to recommend for an equal split.

Sara
0
evilrixSenior Software Engineer (Avast)Commented:
>> and would like to recommend for an equal split.
This would also be my recommendation, as I stated above.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.