gpt4 book ai didi

c++ - 是否有可能覆盖全局实现的功能?

转载 作者:行者123 更新时间:2023-11-27 22:42:32 26 4
gpt4 key购买 nike

我很好奇是否可以覆盖已实现的函数。
我的意思是,是否有任何允许替代实现的函数声明/实现的合法语法?

我为什么要问? (我知道这听起来很荒谬)
首先,只是出于好奇和扩展我的知识。
其次,我了解到全局 new 可以被覆盖(尽管强烈不推荐)。
第三,假设我写了一个库:AwsomeLibrary.hpp,它我的 friend 想包括。
在很多函数中,有一个像void sort(int* arr)这样的函数,他认为他可以更好地实现它(当然称之为同名)。

最佳答案

I mean, is there any legal syntax of function declaration / implementation that allows alternative implementation?

没有。这将打破单一定义规则 (ODR)。

Second, I've learned that the global new can be overrided (Although it is strongly not recommended).

可替换分配函数,记录在 http://en.cppreference.com/w/cpp/memory/new/operator_new实际上只是一个非常特殊的情况,语言和标准库之间的灰色地带;当然不是您可以从中推断出您自己的代码的一般规则的东西。

Third, assume that I have written a library: AwsomeLibrary.hpp, which my friend wants to include. Among a lot of functions, there is a function like void sort(int* arr), which he thinks that he could implement better (and of course call it with the same name).

此类问题超出了 C++ 的范围。它们与源代码控制版本控制系统更相关,如 Git .例如,如果您的项目在 Git 控制下,那么您的 friend 可以使用他更好的实现创建一个代码分支。

关于c++ - 是否有可能覆盖全局实现的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47462789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com