0

Here are my projects files compiled with VS:

Init.h

#pragma once

#include <iostream>
#include <memory>

#include "Ishare.h"

using namespace std;

class Init {
private:
  std::unique_ptr<IfShare> p_ifShare;
  int constData;
  int varData;

public:

  Init(const IfShare& ifshareInit, int constDataInit) :
    p_ifShare(std::make_unique<IfShare>(ifshareInit)) {
    varData = 10;
    cout << "Init parameterized constructor called" << endl;
    cout << "varData:" << varData << "constDataInit:" << constDataInit << endl;
  }

  Init(const Init& initInstance) : 
    p_ifShare(std::make_unique<IfShare>(initInstance.p_ifShare)),
    constData(initInstance.constData){
    varData = 100;
    cout << "Init copy constructor called" << endl;
    cout << "varData:" << varData << "constDataInit:" << initInstance.constData << endl;
  }
};

void InitFunc();

Init.cpp

#include "Init.h"
#include "share.h"

void InitFunc() {
  std::unique_ptr<Init> InitImpl = std::make_unique<Init>(InitShareFunc());
}

Ishare.h

#pragma once

class IfShare {
public:
  virtual void sharePrint1() = 0;
  virtual void sharePrint2() = 0;
};

share.h

#pragma once

#include <iostream>
#include "Ishare.h"
#include "Init.h"

class Share : public IfShare {
public:
  void sharePrint1() {
    std::cout << "sharePrint1" << std::endl;
  }
  void sharePrint2() {
    std::cout << "sharePrint2" << std::endl;
  }
};

class App {
public:
  App(const Share& IfShareData, int data) {
    InitShare = std::make_unique<Init>(IfShareData, data);
  }

  friend const Init& InitShareFunc();

private:
  std::unique_ptr<Init> InitShare;
};

mainApp.cpp

#include <iostream>
#include <memory>

#include "share.h"

using namespace std;

static std::unique_ptr<App> p_App;
static std::unique_ptr<Share> p_Share;

const Init& InitShareFunc() {
  return *(p_App->InitShare.get());
}

int main()
{
  int x = 20;
  p_Share = std::make_unique<Share>();
  p_App = std::make_unique<App>(*(p_Share.get()), x);
  InitFunc(); // In user application it should call by external plugin, 
              //for testing I placed it here
  return 0;
}

main app: should initialize the shared object and pass it to the App Object

App: Initialize the Init Object with unique membership and share it with the friend function.

IShare: pure virtual class to route the calls from Init to Share class

Init: Initialize the Init object reference from App Init Object and call member functions of share via iShare.

The basic idea is to have unique membership of Init Object with AppInstance and InitFunc() and always create a local Init Instance from it.

Facing the following errors and can't understand why??

Severity    Code    Description Project File    Line
Error (active)      member "Init::p_ifShare" (declared at line 12 of "c:\Users\nxf92615\Documents\Visual Studio 2015\Projects\Project3\Project3\Init.h") is inaccessible    Project3    c:\Users\nxf92615\Documents\Visual Studio 2015\Projects\Project3\Project3\Init.cpp  6
Error   C2259   'IfShare': cannot instantiate abstract class    Project3    c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory   1636
Error   C2664   'IfShare::IfShare(IfShare &&)': cannot convert argument 1 from 'const std::unique_ptr<IfShare,std::default_delete<_Ty>>' to 'const IfShare &'   Project3    c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory   1636
Error   C2259   'IfShare': cannot instantiate abstract class    Project3    c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory   1636
Error   C2664   'IfShare::IfShare(IfShare &&)': cannot convert argument 1 from 'const std::unique_ptr<IfShare,std::default_delete<_Ty>>' to 'const IfShare &'   Project3    c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory   1636

chandu
  • 65
  • 1
  • 8
  • Why do you pass `const IfShare&` if you want a `std::unique_ptr`? Do you really intend to copy `ifshareInit` in `Init` constructor? What do you intend to do with `p_ifShare` in `Init` copy constructor? – Yksisarvinen Jun 22 '23 at 11:32
  • I want to keep ownership with p_Share, don't want to move. p_ifShare is to call the Share functionality. It is a part of my border project. – chandu Jun 22 '23 at 11:47
  • 1
    not being able to instantiate it is what makes a class abstract – 463035818_is_not_an_ai Jun 22 '23 at 11:52
  • Well, then you can't use `std::unique_ptr`. As the name suggests, it only allows one owner. If `Init` is not owner of `p_ifShare`, make it a raw pointer or a reference (though that will disable assignment operations). – Yksisarvinen Jun 22 '23 at 11:59
  • @Yksisarvinen How can I design this code without moving ownership? or sharing the owner ship? – chandu Jun 22 '23 at 14:11

0 Answers0