logo
down
shadow

How to work with std::make_unique function and interface classes?


How to work with std::make_unique function and interface classes?

By : Paddy Lee
Date : November 21 2020, 11:01 PM
help you fix your problem There are three major problems with your code.
1) do is a keyword of the language. You cannot use it as an identifier (like function name)
code :
Doer(std::unique_ptr<Base> b_ptr) : ptr(std::move(b_ptr)) {}
Doer doer(std::make_unique<A>());


Share : facebook icon twitter icon
Can std::make_unique be used with abstract interface?

Can std::make_unique be used with abstract interface?


By : user2569496
Date : March 29 2020, 07:55 AM
With these it helps Yes, you can of course use make_unique for that, but it's not as useful as you might want. You have these options:
code :
std::unique_ptr<IGpsSource> source1 = std::make_unique<GpsDevice>(comPort, baudrate);
auto source2 = std::unique_ptr<IGpsSource>{ std::make_unique<GpsLog>(filename) };
How work with different classes from 1 interface (class)

How work with different classes from 1 interface (class)


By : Dario
Date : March 29 2020, 07:55 AM
may help you . Example to eloborate on my comment. As you can see, if u extend all from the same interface/class you can just call the method without testing the actual type as long as you know its of the super type Foo, in this case
code :
<?php

class Foo {
    protected $name;

    public function __construct($n) {
        $this->name = $n;
    }

    public function getName() {
        return $this->name;
    }
}

class Bar extends Foo {

}

class FooBar extends Foo {
    public function getName() {
        return strtoupper($this->name);
    }
}

function sayName(Foo $f) {
    return 'Hello '. $f->getName();
}

$instances = array(
    new Foo('Foo'),
    new Bar('Bar'),
    new FooBar('FooBar'),
);

foreach($instances as $instance) {
    echo sayName($instance).'<br />'.PHP_EOL;
}
How does returning std::make_unique<SubClass> work?

How does returning std::make_unique<SubClass> work?


By : Ben Rubert
Date : March 29 2020, 07:55 AM
should help you out std::unique_ptr is not copyable, only movable. The reason you can return std::make_unique from a function declared to return std::unique_ptr is that there is a conversion from one to the other.
So 1) is equivalent to:
code :
std::unique_ptr<Base> GetDerived() {
    return std::unique_ptr<Base>(std::made_unique<Derived>());
}
std::unique_ptr<Base> GetDerived2() { 
    std::unique_ptr<Derived> a = std::make_unique<Derived>(); 
    return std::unique_ptr<Base>(a); 
}
Making two classes work together in an interface

Making two classes work together in an interface


By : Mahabub
Date : March 29 2020, 07:55 AM
I hope this helps you . There were several issues in the code provided in the OP, I've taken the liberty of refactoring the code as follows (verified using the following Java IDE):
code :
public interface IntegerExpression {
    int getValue();
}

public abstract class TwoOperandOperator implements IntegerExpression {
   protected IntegerExpression leftNum; 
   protected IntegerExpression rightNum;
   protected TwoOperandOperator(IntegerExpression leftNum, IntegerExpression rightNum) {
      this.leftNum = leftNum;
      this.rightNum = leftNum;
   }
}

public class IntegerConstant implements IntegerExpression {
   private int val;
   @Override
   public int getValue(){
      return val;
   }  
   public /*int*/ IntegerConstant(int val){
      this.val = val;
      // No need for the code below - constructors do not return values 
      // return val;
   } 
}


public class Addition extends TwoOperandOperator {
   public Addition(IntegerExpression leftNum, IntegerExpression rightNum)
   {
      super(leftNum,rightNum);
   }
   @Override
   public int getValue(){
      return (leftNum.getValue() + rightNum.getValue());
   }
}


public class Multiplication extends TwoOperandOperator {
   public Multiplication (IntegerExpression leftNum, IntegerExpression rightNum)
   {
      super(leftNum,rightNum);
   }
   @Override
   public int getValue(){
        return (leftNum.getValue() * rightNum.getValue());
   }
}
Difference between make_unique and make_unique with reference_wrapper in c++

Difference between make_unique and make_unique with reference_wrapper in c++


By : neilotoole
Date : March 29 2020, 07:55 AM
hope this fix your issue , std::make_unique
returns
Related Posts Related Posts :
  • Referring to a CMAKE variable from code
  • Convert a CString to an array of floats
  • Generating a sequence of zeros at compile time
  • Pass std::iterator pointing to a vector of Eigen types
  • Overloaded pointer to function
  • How to visualise a simple std::string with natvis?
  • OpenCV: can't access Mat elements
  • What happens when you redefine a macro?
  • use the TYPE from the tempate type argument list
  • Warning: Multiple copy constructors defined while deleting them
  • xgboost build error on CentOS 6.8
  • How can I sort elements of a vector by row using std::sort
  • Using an std::function for wrapping a function object
  • How to add a Combobox or browse option in one particular row of a grid control using CMFCPropertyGridProperty class
  • update curl on centos
  • How can I use Linguist in Qt without .pro file?
  • reduce data to be sent over UDP sockets
  • cudaModuleLoadData fails with error code 201
  • C++ code not compiling with gcc on Mac?
  • Editing the value in an unordered map for a given key
  • How do call C++ function through Delphi wrapper
  • How could I link the Boost (not in usr/include) in CMakeList.txt using find_package
  • C++, Linker Issue returning undefined reference
  • Can you use std::string in a 3rd party dll?
  • Why does this use of std::sort with a custom comparator not compile?
  • Fixed base address in MSVC2015
  • Armadillo C++ Matrix Library - simplest way to initialize all values in a matrix to 999?
  • How do you end while loop
  • C++ Basic While Loop: Unknown Inputs and Value Increment
  • Cmake build failed with CMAKE_AR-NOTFOUND (cr exe not found)
  • c++ typeid on class member operator() overloads
  • Different size of string showing when going by two different methods
  • what the meaning "source of entropy of sth." on stack
  • wxWidgets - wxStyledTextCtrl - Text alignment RTL
  • get the entry point to a 64bit process memory from a 32bit process
  • Stroustrup: For C++, how do I install FLTK library?
  • Getting a word after a certain character?
  • How can I convert a char vector to a char array?
  • Comparing vectors of different length c++
  • How do I add four seperate times together?
  • How to replace data in a certain node in a linked list?
  • Prepared statements in C++ libneo4j_client?
  • FindFirstVolume does not return EFI system partition
  • User Inputs Value, but Arbitrary Value Printed to Screen. Printed Value doesn't Match Entered Value
  • Refresh image in command line (C++)
  • How to flip a Char array with pointers in C++
  • Outputting time in minutes and seconds C++
  • Creating a for loop to continue to count
  • how to get to field of 2D object dynamic array?
  • Multiple class object share common variable
  • Why is my multi-threaded program not terminating properly?
  • How do I open a file in C++ using just the filename?
  • QT DOMXml - Change the name of a node
  • Using undeclared list as argument in c++
  • While loop won't continue
  • If/else if always goes to else statement
  • Why shared_timed_mutex is defined in c++14, but shared_mutex in c++17?
  • Confused by pipes. 'cat -A' seems to filter out part of output
  • Cannot open source file gtest/gtest.h
  • How to get a certain word in string of getline?
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com