How to avoid calling function in base class?
I have two classes A
and B
like below, B
have to inherit from A
. The classType()
function of class B
is not provided, If call B::classType()
, will get the class type of A
. I would like to get a compiler error here instead of calling into the classType()
function of class A
. How do I do it?
In general, the sub classes of A
should provide the function classType()
, in case the author forgets to write the classType()
function, it's best to report a compiler error. This is the purpose. Maybe, I can use static_assert
in TypeOf()
function.
class A
public:
static const char* classType()
static const char* s_classType = "A";
return s_classType;
;
class B : public A
;
template <class T>
const char* TypeOf()
return T::classType();
int main()
const char* type = TypeOf<B>(); // I would like to get compiler error here, not get "A".
c++
|
show 4 more comments
I have two classes A
and B
like below, B
have to inherit from A
. The classType()
function of class B
is not provided, If call B::classType()
, will get the class type of A
. I would like to get a compiler error here instead of calling into the classType()
function of class A
. How do I do it?
In general, the sub classes of A
should provide the function classType()
, in case the author forgets to write the classType()
function, it's best to report a compiler error. This is the purpose. Maybe, I can use static_assert
in TypeOf()
function.
class A
public:
static const char* classType()
static const char* s_classType = "A";
return s_classType;
;
class B : public A
;
template <class T>
const char* TypeOf()
return T::classType();
int main()
const char* type = TypeOf<B>(); // I would like to get compiler error here, not get "A".
c++
Preferstd::string
toconst char*
.
– Ron
Nov 15 '18 at 16:09
void main
?? -
– Quentin
Nov 15 '18 at 16:11
Solution: replaceclass B : public A
withclass B
.
– Evg
Nov 15 '18 at 16:12
@Evg, B needs to inherits form A in the case.
– ldlchina
Nov 15 '18 at 16:14
1
See edited answer.
– Evg
Nov 15 '18 at 16:39
|
show 4 more comments
I have two classes A
and B
like below, B
have to inherit from A
. The classType()
function of class B
is not provided, If call B::classType()
, will get the class type of A
. I would like to get a compiler error here instead of calling into the classType()
function of class A
. How do I do it?
In general, the sub classes of A
should provide the function classType()
, in case the author forgets to write the classType()
function, it's best to report a compiler error. This is the purpose. Maybe, I can use static_assert
in TypeOf()
function.
class A
public:
static const char* classType()
static const char* s_classType = "A";
return s_classType;
;
class B : public A
;
template <class T>
const char* TypeOf()
return T::classType();
int main()
const char* type = TypeOf<B>(); // I would like to get compiler error here, not get "A".
c++
I have two classes A
and B
like below, B
have to inherit from A
. The classType()
function of class B
is not provided, If call B::classType()
, will get the class type of A
. I would like to get a compiler error here instead of calling into the classType()
function of class A
. How do I do it?
In general, the sub classes of A
should provide the function classType()
, in case the author forgets to write the classType()
function, it's best to report a compiler error. This is the purpose. Maybe, I can use static_assert
in TypeOf()
function.
class A
public:
static const char* classType()
static const char* s_classType = "A";
return s_classType;
;
class B : public A
;
template <class T>
const char* TypeOf()
return T::classType();
int main()
const char* type = TypeOf<B>(); // I would like to get compiler error here, not get "A".
c++
c++
edited Nov 15 '18 at 19:41
Evg
4,01221436
4,01221436
asked Nov 15 '18 at 16:08
ldlchinaldlchina
4161823
4161823
Preferstd::string
toconst char*
.
– Ron
Nov 15 '18 at 16:09
void main
?? -
– Quentin
Nov 15 '18 at 16:11
Solution: replaceclass B : public A
withclass B
.
– Evg
Nov 15 '18 at 16:12
@Evg, B needs to inherits form A in the case.
– ldlchina
Nov 15 '18 at 16:14
1
See edited answer.
– Evg
Nov 15 '18 at 16:39
|
show 4 more comments
Preferstd::string
toconst char*
.
– Ron
Nov 15 '18 at 16:09
void main
?? -
– Quentin
Nov 15 '18 at 16:11
Solution: replaceclass B : public A
withclass B
.
– Evg
Nov 15 '18 at 16:12
@Evg, B needs to inherits form A in the case.
– ldlchina
Nov 15 '18 at 16:14
1
See edited answer.
– Evg
Nov 15 '18 at 16:39
Prefer
std::string
to const char*
.– Ron
Nov 15 '18 at 16:09
Prefer
std::string
to const char*
.– Ron
Nov 15 '18 at 16:09
void main
?? -– Quentin
Nov 15 '18 at 16:11
void main
?? -– Quentin
Nov 15 '18 at 16:11
Solution: replace
class B : public A
with class B
.– Evg
Nov 15 '18 at 16:12
Solution: replace
class B : public A
with class B
.– Evg
Nov 15 '18 at 16:12
@Evg, B needs to inherits form A in the case.
– ldlchina
Nov 15 '18 at 16:14
@Evg, B needs to inherits form A in the case.
– ldlchina
Nov 15 '18 at 16:14
1
1
See edited answer.
– Evg
Nov 15 '18 at 16:39
See edited answer.
– Evg
Nov 15 '18 at 16:39
|
show 4 more comments
4 Answers
4
active
oldest
votes
Alternatively, make A::classType()
private in B
:
class B : public A
private:
using A::classType;
Edit. After the question has been clarified, I suggest that you use type traits technique instead of static member functions.
template<class>
struct Traits;
template<>
struct Traits<A>
static const char* classType()
return "A";
;
template<class T>
const char* TypeOf()
return Traits<T>::classType();
Now the user has to define his own type trait for B
, otherwise
const char* type = TypeOf<B>();
will not compile.
Explicitly deleting the function in classB
seems more expressive to me. Both approaches work with classes inheriting fromB
.
– Paul Sanders
Nov 15 '18 at 16:19
@PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.
– Evg
Nov 15 '18 at 16:20
add a comment |
Apart from a couple of typos in your code ... add the following to the definition of class B
static const char* classType() = delete;
Live Demo
It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.
– ldlchina
Nov 15 '18 at 16:30
add a comment |
I got the idea from @Evg, thanks Evg.
Use template like below:
class A
public:
template <class T>
static const char* classType() static_assert(false, "classType not implemented");
template <>
static const char* classType<A>()
static const char* s_classType = "A";
return s_classType;
;
template <class T>
const char* TypeOf()
return T::classType<T>();
add a comment |
Declare the function as pure virtual.
class A
public:
virtual static const char* classType() =0;
;
Then all instantiated classes inheriting from A have to implement this.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323485%2fhow-to-avoid-calling-function-in-base-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Alternatively, make A::classType()
private in B
:
class B : public A
private:
using A::classType;
Edit. After the question has been clarified, I suggest that you use type traits technique instead of static member functions.
template<class>
struct Traits;
template<>
struct Traits<A>
static const char* classType()
return "A";
;
template<class T>
const char* TypeOf()
return Traits<T>::classType();
Now the user has to define his own type trait for B
, otherwise
const char* type = TypeOf<B>();
will not compile.
Explicitly deleting the function in classB
seems more expressive to me. Both approaches work with classes inheriting fromB
.
– Paul Sanders
Nov 15 '18 at 16:19
@PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.
– Evg
Nov 15 '18 at 16:20
add a comment |
Alternatively, make A::classType()
private in B
:
class B : public A
private:
using A::classType;
Edit. After the question has been clarified, I suggest that you use type traits technique instead of static member functions.
template<class>
struct Traits;
template<>
struct Traits<A>
static const char* classType()
return "A";
;
template<class T>
const char* TypeOf()
return Traits<T>::classType();
Now the user has to define his own type trait for B
, otherwise
const char* type = TypeOf<B>();
will not compile.
Explicitly deleting the function in classB
seems more expressive to me. Both approaches work with classes inheriting fromB
.
– Paul Sanders
Nov 15 '18 at 16:19
@PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.
– Evg
Nov 15 '18 at 16:20
add a comment |
Alternatively, make A::classType()
private in B
:
class B : public A
private:
using A::classType;
Edit. After the question has been clarified, I suggest that you use type traits technique instead of static member functions.
template<class>
struct Traits;
template<>
struct Traits<A>
static const char* classType()
return "A";
;
template<class T>
const char* TypeOf()
return Traits<T>::classType();
Now the user has to define his own type trait for B
, otherwise
const char* type = TypeOf<B>();
will not compile.
Alternatively, make A::classType()
private in B
:
class B : public A
private:
using A::classType;
Edit. After the question has been clarified, I suggest that you use type traits technique instead of static member functions.
template<class>
struct Traits;
template<>
struct Traits<A>
static const char* classType()
return "A";
;
template<class T>
const char* TypeOf()
return Traits<T>::classType();
Now the user has to define his own type trait for B
, otherwise
const char* type = TypeOf<B>();
will not compile.
edited Nov 15 '18 at 16:39
answered Nov 15 '18 at 16:15
EvgEvg
4,01221436
4,01221436
Explicitly deleting the function in classB
seems more expressive to me. Both approaches work with classes inheriting fromB
.
– Paul Sanders
Nov 15 '18 at 16:19
@PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.
– Evg
Nov 15 '18 at 16:20
add a comment |
Explicitly deleting the function in classB
seems more expressive to me. Both approaches work with classes inheriting fromB
.
– Paul Sanders
Nov 15 '18 at 16:19
@PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.
– Evg
Nov 15 '18 at 16:20
Explicitly deleting the function in class
B
seems more expressive to me. Both approaches work with classes inheriting from B
.– Paul Sanders
Nov 15 '18 at 16:19
Explicitly deleting the function in class
B
seems more expressive to me. Both approaches work with classes inheriting from B
.– Paul Sanders
Nov 15 '18 at 16:19
@PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.
– Evg
Nov 15 '18 at 16:20
@PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.
– Evg
Nov 15 '18 at 16:20
add a comment |
Apart from a couple of typos in your code ... add the following to the definition of class B
static const char* classType() = delete;
Live Demo
It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.
– ldlchina
Nov 15 '18 at 16:30
add a comment |
Apart from a couple of typos in your code ... add the following to the definition of class B
static const char* classType() = delete;
Live Demo
It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.
– ldlchina
Nov 15 '18 at 16:30
add a comment |
Apart from a couple of typos in your code ... add the following to the definition of class B
static const char* classType() = delete;
Live Demo
Apart from a couple of typos in your code ... add the following to the definition of class B
static const char* classType() = delete;
Live Demo
answered Nov 15 '18 at 16:13
Paul SandersPaul Sanders
5,2762621
5,2762621
It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.
– ldlchina
Nov 15 '18 at 16:30
add a comment |
It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.
– ldlchina
Nov 15 '18 at 16:30
It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.
– ldlchina
Nov 15 '18 at 16:30
It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.
– ldlchina
Nov 15 '18 at 16:30
add a comment |
I got the idea from @Evg, thanks Evg.
Use template like below:
class A
public:
template <class T>
static const char* classType() static_assert(false, "classType not implemented");
template <>
static const char* classType<A>()
static const char* s_classType = "A";
return s_classType;
;
template <class T>
const char* TypeOf()
return T::classType<T>();
add a comment |
I got the idea from @Evg, thanks Evg.
Use template like below:
class A
public:
template <class T>
static const char* classType() static_assert(false, "classType not implemented");
template <>
static const char* classType<A>()
static const char* s_classType = "A";
return s_classType;
;
template <class T>
const char* TypeOf()
return T::classType<T>();
add a comment |
I got the idea from @Evg, thanks Evg.
Use template like below:
class A
public:
template <class T>
static const char* classType() static_assert(false, "classType not implemented");
template <>
static const char* classType<A>()
static const char* s_classType = "A";
return s_classType;
;
template <class T>
const char* TypeOf()
return T::classType<T>();
I got the idea from @Evg, thanks Evg.
Use template like below:
class A
public:
template <class T>
static const char* classType() static_assert(false, "classType not implemented");
template <>
static const char* classType<A>()
static const char* s_classType = "A";
return s_classType;
;
template <class T>
const char* TypeOf()
return T::classType<T>();
edited Nov 16 '18 at 5:31
answered Nov 15 '18 at 17:34
ldlchinaldlchina
4161823
4161823
add a comment |
add a comment |
Declare the function as pure virtual.
class A
public:
virtual static const char* classType() =0;
;
Then all instantiated classes inheriting from A have to implement this.
add a comment |
Declare the function as pure virtual.
class A
public:
virtual static const char* classType() =0;
;
Then all instantiated classes inheriting from A have to implement this.
add a comment |
Declare the function as pure virtual.
class A
public:
virtual static const char* classType() =0;
;
Then all instantiated classes inheriting from A have to implement this.
Declare the function as pure virtual.
class A
public:
virtual static const char* classType() =0;
;
Then all instantiated classes inheriting from A have to implement this.
answered Nov 15 '18 at 16:56
Robert AndrzejukRobert Andrzejuk
2,99621425
2,99621425
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323485%2fhow-to-avoid-calling-function-in-base-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Prefer
std::string
toconst char*
.– Ron
Nov 15 '18 at 16:09
void main
?? -– Quentin
Nov 15 '18 at 16:11
Solution: replace
class B : public A
withclass B
.– Evg
Nov 15 '18 at 16:12
@Evg, B needs to inherits form A in the case.
– ldlchina
Nov 15 '18 at 16:14
1
See edited answer.
– Evg
Nov 15 '18 at 16:39