c++ - Is it possible to assign two structure of different type? -


my need assign similar structure another, names different. if of same name use =(assignment) directly. dont want use memcopy copy bits.

struct first{ int i; char c; } struct second{ int i; char c; //we can overload assignment operator copy field. void operator = ( struct first& f)     i=f.i;     c=f.c; }  int main() {   struct first f;   f.i=100;   f.c='a';   struct second s=f;  } 

but getting compilation error. error: conversion "first" non-scalar type "second" requested.

not sure if possible.

you need constructor use

struct second s=f; 

such as:

struct second{   int i;   char c;   second(first const& f) : i(f.i), c(f.c) {}    ...  }; 

to use assignment operator, use:

second s;  // no need use struct in c++ s = f; 

btw, proper interface , implementation operator= function should be:

second& operator=(first const& f) {    i=f.i;    c=f.c;    return *this; } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -