首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对象文件中的ostream操作符会发生什么情况?

对象文件中的ostream操作符会发生什么情况?
EN

Stack Overflow用户
提问于 2015-10-12 16:49:49
回答 1查看 209关注 0票数 1

我想了解类‘ostream’操作符在对象文件中的位置。在下面的代码示例中,我设计了C++类DeltaTimer,它是一个ostream操作符的朋友。下面是"nm DeltaTimer.o“的输出。这个输出中的"ostream“显示了它们与符号类型"U”对应的情况,即未定义。但是代码编译成功(为了完整性而包括main.cpp)和链接,并按预期执行,这意味着没有任何未定义的内容。但是我不明白在"nm“的输出中,ostream操作符想要做什么,如果有人能给出建议的话,我会很感激。谢谢。

DeltaTimer.h:

代码语言:javascript
复制
#ifndef DeltaTimer_h
#define DeltaTimer_h

#include <iostream>

class DeltaTimer
{
public:
    DeltaTimer();
    virtual ~DeltaTimer();
    void Foo();

private:
    int m_foo;

friend std::ostream& operator<<(std::ostream& os, const DeltaTimer& rDeltaTimer);
};

#endif // DeltaTimer_h

DeltaTimer.cpp:

代码语言:javascript
复制
#include "DeltaTimer.h"
#include <iostream>

DeltaTimer::DeltaTimer() : m_foo(5)
{
    std::cout << "DeltaTimer ctor" << std::endl;
}

DeltaTimer::~DeltaTimer()
{
    std::cout << "DeltaTimer dtor" << std::endl;
}

void DeltaTimer::Foo()
{
    std::cout << "DeltaTimer.m_foo == " << m_foo << std::endl;
}

std::ostream& operator<<(std::ostream& os, const DeltaTimer& rDeltaTimer)
{
    os << "DeltaTimer[m_foo(" << rDeltaTimer.m_foo << ")]" << std::endl;
}

// This function is just a sanity-check for the output of "nm"
void Bar()
{
    std::cout << "void Bar()" << std::endl;
}

main.cpp:

代码语言:javascript
复制
#include "DeltaTimer.h"
#include <iostream>

int main()
{
    DeltaTimer deltaTimer;

    std::cout << deltaTimer << std::endl;
}

“纳米”的输出:

代码语言:javascript
复制
>nm DeltaTimer.o
00000000000001a7 t _GLOBAL__I__ZN10DeltaTimerC2Ev
0000000000000145 T _Z3Barv
0000000000000167 t _Z41__static_initialization_and_destruction_0ii
00000000000000b0 T _ZN10DeltaTimer3FooEv
0000000000000000 T _ZN10DeltaTimerC1Ev
0000000000000000 T _ZN10DeltaTimerC2Ev
000000000000008a T _ZN10DeltaTimerD0Ev
0000000000000040 T _ZN10DeltaTimerD1Ev
0000000000000040 T _ZN10DeltaTimerD2Ev
                 U _ZNSolsEPFRSoS_E
                 U _ZNSolsEi
                 U _ZNSt8ios_base4InitC1Ev
                 U _ZNSt8ios_base4InitD1Ev
                 U _ZSt4cout
                 U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
0000000000000000 b _ZStL8__ioinit
                 U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
0000000000000000 V _ZTI10DeltaTimer
0000000000000000 V _ZTS10DeltaTimer
0000000000000000 V _ZTV10DeltaTimer
                 U _ZTVN10__cxxabiv117__class_type_infoE
                 U _ZdlPv
00000000000000f1 T _ZlsRSoRK10DeltaTimer
                 U __cxa_atexit
                 U __dso_handle
                 U __gxx_personality_v0

>nm DeltaTimer.o | grep -i ostream
                 U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
                 U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-12 16:54:13

这是因为nm显示的名字很烂。要打印人类可读的参数,您需要使用--demangle参数:

代码语言:javascript
复制
$ nm --demangle DeltaTimer.o 
                 U __cxa_atexit
                 U __dso_handle
00000000000001a4 t _GLOBAL__sub_I__ZN10DeltaTimerC2Ev
                 U __gxx_personality_v0
0000000000000145 T Bar()
0000000000000167 t __static_initialization_and_destruction_0(int, int)
                 U operator delete(void*)
00000000000000f1 T operator<<(std::ostream&, DeltaTimer const&) // <== here's your guy!
00000000000000b0 T DeltaTimer::Foo()
0000000000000000 T DeltaTimer::DeltaTimer()
0000000000000000 T DeltaTimer::DeltaTimer()
000000000000008a T DeltaTimer::~DeltaTimer()
0000000000000040 T DeltaTimer::~DeltaTimer()
0000000000000040 T DeltaTimer::~DeltaTimer()
                 U std::ostream::operator<<(int)
                 U std::ostream::operator<<(std::ostream& (*)(std::ostream&))
                 U std::ios_base::Init::Init()
                 U std::ios_base::Init::~Init()
                 U std::cout
                 U std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
0000000000000000 r std::piecewise_construct
0000000000000000 b std::__ioinit
                 U std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
0000000000000000 V typeinfo for DeltaTimer
0000000000000000 V typeinfo name for DeltaTimer
0000000000000000 V vtable for DeltaTimer
                 U vtable for __cxxabiv1::__class_type_info

或者直接使用c++filt是为了。具体地说:

代码语言:javascript
复制
$ echo _ZlsRSoRK10DeltaTimer | c++filt
operator<<(std::basic_ostream<char, std::char_traits<char> >&, DeltaTimer const&)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33086299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档