-1

I am following an online tutorial which is quite similar to my code, but not exactly the same. I have modified it so that I can have a better understanding of it, but I am having problems getting/returning the string value from the inline function. The string value becomes empty as soon as the compiler exits the last curly bracket of Another.cpp.

Is there any way to solve my issue without changing the structure of my files as well as the code? Particularly, I don't want to change anything from the Another.h. Please, if someone can explain what could be the reason behind the inline function here as well, will really appreciate that.

Another.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;

    };
}

Another.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;
        }*/
    };

}

main.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();
}

Extra

Inside of Another.cpp, I have also tried the following code:

std::string  Ast = "asdf";
Outside::Inside* ob = new Outside::Inside(Ast);
auto s_String = &(ob->Get());
8
  • 3
    #pragma once in your main.cpp is very odd. Commented Aug 5, 2020 at 15:29
  • i have removed it and I forgot tot add that i was getting 00000000 as the answer, as the s_String value is getting empty as soon as the compiler exit the last curly bracket of Another.cpp Commented Aug 5, 2020 at 15:33
  • 1
    I think provided too much code especially since much of it is commented out. Commented Aug 5, 2020 at 15:34
  • inline static std::shared_ptr<Outside::Inside>& GetString(... is odd, in my opinion. Should either return std::shared_ptr<Outside::Inside> or Outside::Inside const& or Outside::Inside or std::string. Commented Aug 5, 2020 at 15:38
  • 1
    This is breaking my brain. Can you simplify it please Commented Aug 5, 2020 at 16:45

1 Answer 1

3

In Test::Init, this line:

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

declares a local variable s_String that shadows the static member variable. So when you exit the function, you are seeing the original value of the static member variable, which was never modified.

Instead, simply do:

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

to assign to the static member variable. Since s_String is a shared_ptr, what you're looking for is probably:

s_String = std::make_shared(Outside::Inside("asdf"));
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, I have already tried it, and it doesn't work., I can't even use s_String without the auto either.
What happens when you try that? Do you get a compiler error?
I forgot tot add that i was getting 00000000 as the answer, as the s_String value is getting empty as soon as the compiler exit the last curly bracket of Another.cpp also when i used this //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());
Another.cpp probably should have: s_String = std::make_shared<Outside::Inside>("asdf");
before getting outside of the curly bracket from Another.cpp the compiler is showing that auto s_String is storing the correct value but after the curly bracket it becomes empty.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.