Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6605039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:14:09+00:00 2026-05-25T19:14:09+00:00

The following code fails to compile in Visual C++ 11 with this error: 1>c:\users\tony\documents\visual

  • 0

The following code fails to compile in Visual C++ 11 with this error:

1>c:\users\tony\documents\visual studio 11\projects\cpp11_ex1\cpp11_ex1\main.cpp(52): error C2440: ” : cannot convert from ‘Foo *const ‘ to ‘std::weak_ptr<_Ty>’

#include <stdio.h>
#include <memory>

using namespace std;

class Foo;

class Bar
{
public:
    Bar( weak_ptr<Foo> foo ) : _foo(foo) { printf("Bar(%p)\n",this); }
    ~Bar() { printf("~Bar(%p)\n",this); }
private:
    weak_ptr<Foo> _foo;
};

class Foo
{
public:
    Foo() : _bar() { _bar = make_shared<Bar>( weak_ptr<Foo>(this) );  printf("Foo(%p)\n",this); }
    ~Foo() { printf("~Foo(%p)\n",this); }
private:
    shared_ptr<Bar> _bar;
};

int main( int argc, char* argv[] )
{
    shared_ptr<Foo> instance = make_shared<Foo>();

    return 0;
}

It seems that I can’t create a weak_ptr from a raw this pointer. This causes an interesting series of problems.

  1. Since I am attempting this in Foo‘s ctor, Foo‘s reference count is 0 (i.e. the make_shared<> in main hasn’t returned yet).

  2. I’ve discovered that I can create weak_ptrs from shared_ptrs… But if I change Bar ctor to take a shared_ptr, I the act of calling Bar‘s constructor ends up destroying Foo! (Since Foo‘s reference count is still 0, creating (and then destroying) a shared_ptr to Foo via a call to Bar‘s ctor ).

All I really want to do is create Foo, have Foo create and own a Bar, but have Bar have a weak reference back to Foo. I really don’t want to be forced into 2 part initialization here!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:14:10+00:00Added an answer on May 25, 2026 at 7:14 pm

    Since Foo will be pointed at by a shared_ptr, and Bar will always be owned by a shared_pointer of Bar, then if Bar exists, Foo exists. Ergo, you don’t need a smart pointer in Bar. (If I understand the problem correctly)

    #include <stdio.h>
    #include <memory>
    
    using namespace std;
    
    class Foo;
    
    class Bar
    {
    public:
        Bar( Foo* foo ) : _foo(foo) { printf("Bar(%p)\n",this); }
        ~Bar() { printf("~Bar(%p)\n",this); }
    private:
        Foo* _foo;
    };
    
    class Foo
    {
    public:
        Foo() : _bar(new Bar(this)) { printf("Foo(%p)\n",this); }
        ~Foo() { printf("~Foo(%p)\n",this); }
    private:
        shared_ptr<Bar> _bar;
    };
    
    int main( int argc, char* argv[] )
    {
        shared_ptr<Foo> instance = make_shared<Foo>();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code doesn't compile with gcc, but does with Visual Studio: template <typename
The following code fails to compile stating A local variable named 'st' cannot be
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
I have the following code and it fails to compile template < typename T
The following code fails to compile with message: cannot instantiate type SymmetricKey SymmetricKey is
Hi trying the compile Objective C++/Protocol code but the compilation fails with error Expected
I am baffled by the linker error when using the following code: // static_const.cpp
The following code fails at runtime… Dim Id As Guid = CType(e.CommandArgument, Guid) It
For some reason the following code fails. You can't simply erase a reverse_iterator by
I'm trying to use opengl in C#. I have following code which fails with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.