【问题标题】:Inline function of my code is not storing string the string value我的代码的内联函数不存储字符串字符串值
【发布时间】:2020-08-05 15:23:59
【问题描述】:

我正在关注一个与我的代码非常相似但不完全相同的在线教程。我已经对其进行了修改,以便更好地理解它,但是我在从内联函数获取/返回字符串值时遇到了问题。一旦编译器退出最后一个大括号Another.cpp,字符串值就会变为空。

有什么方法可以在不更改文件结构和代码的情况下解决我的问题?特别是,我不想更改 Another.h 的任何内容。请,如果有人可以在这里解释内联函数背后的原因,我将不胜感激。

另一个.h

#pragma once

#include<iostream>
#include"Temple.h"
#include<memory>

namespace NamespaceAn
{
    class Test
    {
    public:
        static void Init();
        inline static std::shared_ptr<Outside::Inside>& GetString() { return s_String; }
    private:
        static std::shared_ptr<Outside::Inside> s_String;

    };
}

另一个.cpp

#include "Another.h"
#include<string>

namespace NamespaceAn 
{
    std::shared_ptr<Outside::Inside>Test::s_String;

    void Test::Init()
    {
        //This s_String value is getting disapprear as soon as the compiler get out of the curly bracket

        auto s_String = Outside::Inside("asdf");

        //below all are commented out for the Another.cpp
        //std::string  Ast = "asdf";

        /*std::string Ast = "asdf";
        auto s_String = new Outside::Inside(Ast);*/

        /*Outside::Inside* ob = new Outside::Inside(Ast);
        auto s_String = &(ob->Get());*/
        //                               *****    Attention ****
        //no operator "=" matches the operands and binary '=' no operator found which takes a right-hand ooperand of type std::string*(no acceptable conversion)
        //s_String = &(ob->Get());



        //auto s_String = Ast;

        //auto s_String = Outside::Inside("asdf");
        
        //auto s_String = new Outside::Inside("asdf");

        //s_String = reinterpret_cast<Ast>;
        //*s_String = &(Outside::Inside("asdf"));
        //s_String = reinterpret_cast<Outside::Inside*>(&Outside::Inside("asdf"));

    }
}

Temple.h

#pragma once
#include <string>

namespace Outside
{
    class Inside
    {
        std::string m_String = "tesi";
    public:
        Inside(std::string x) 
        {
            m_String = x;
        }
        std::string Get() 
        {
            return m_String;
        }

        /*std::string* m_String;
    public:
        Inside(std::string &x)
        {
            m_String = &x;
        }
        std::string Get()
        {
            return 
            *m_String;
        }*/


        /*template<typename T>
        static T Set(T x) 
        {
            return T;
        }*/
    };

}

ma​​in.cpp

#pragma once

#include<iostream>
#include"Another.h"
#include<memory>



int main() 
{
    NamespaceAn::Test::Init();

        /*auto asdf = NamespaceAn::Test::GetString();
        std::cout << asdf << std::endl;*/

        std::cout << NamespaceAn::Test::GetString();
    
    std::cin.get();
}

额外

Another.cpp里面,我也试过下面的代码:

std::string  Ast = "asdf";
Outside::Inside* ob = new Outside::Inside(Ast);
auto s_String = &(ob->Get());

【问题讨论】:

  • #pragma once 在你的main.cpp 中很奇怪。
  • 我已经删除了它,我忘了补充说我得到了 00000000 作为答案,因为一旦编译器退出 Another.cpp 的最后一个大括号,s_String 值就会变空
  • 我认为提供了太多代码,尤其是因为其中大部分都被注释掉了。
  • inline static std::shared_ptr&lt;Outside::Inside&gt;&amp; GetString(... 在我看来很奇怪。应该返回std::shared_ptr&lt;Outside::Inside&gt;Outside::Inside const&amp;Outside::Insidestd::string
  • 这让我脑洞大开。你能简化一下吗

标签: c++ oop inline-functions


【解决方案1】:

Test::Init,这一行:

auto s_String = Outside::Inside("asdf");

声明一个局部变量s_String,它隐藏静态成员变量。所以当你退出函数时,你会看到静态成员变量的原始值,它从未被修改过。

相反,只需这样做:

s_String = Outside::Inside("asdf");

分配给静态成员变量。由于s_Stringshared_ptr,因此您要查找的可能是:

s_String = std::make_shared(Outside::Inside("asdf"));

【讨论】:

  • 谢谢,我已经试过了,还是不行。,没有auto我什至不能用s_String。
  • 当你尝试这样做时会发生什么?你得到一个编译器错误吗?
  • 我忘了补充说我得到了 00000000 作为答案,因为当我使用这个 //no 时,当编译器退出 Another.cpp 的最后一个大括号时,s_String 值就会变空运算符 "=" 匹配操作数和二进制 '=' 未找到采用 std::string*(无可接受的转换) 类型的右手操作数的运算符 //s_String = &(ob->Get());跨度>
  • 另一个.cpp 可能应该有:s_String = std::make_shared&lt;Outside::Inside&gt;("asdf");
  • 在从 Another.cpp 超出大括号之前,编译器显示 auto s_String 正在存储正确的值,但在大括号之后它变为空。
猜你喜欢
  • 2019-06-01
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多