Sponsored Content
Top Forums Programming How to initialize an object with another object of different class? Post 302886051 by Corona688 on Wednesday 29th of January 2014 12:55:14 PM
Old 01-29-2014
Your 'operator B' doesn't quite work, so it can't convert.

Making an operator there is tricky, because A doesn't know what the contents of B are yet... So you just have to say that the function exists inside the class, then put the function definition after B is defined.

Code:
#include <iostream>
using namespace std;

class B;
class A{
 public:
  A() { cout <<"\nA()" << endl; }

  A(A& rhs) { cout <<"\nCopyCtr:A&" << endl; }

  A& operator=(A& rhs){ cout <<"\nAssignment: A&" << endl; }

  operator B();
};

class B{
 public:
  B() { cout <<"\nB()" << endl; }

  B(const B& rhs) { cout <<"\nCopyCtrB&" << endl; }

  B& operator=(B& rhs) { cout <<"\nAssignment: B&" << endl; }
};

A::operator B() {
        B smplb;
        return(smplb);
}

int main(){
  A a;
  B b = a;
  return 0;
}


Last edited by Corona688; 01-29-2014 at 02:00 PM..
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Programming

create object

can we create an parametrize object to a class with the help of new operator if yes then how (0 Replies)
Discussion started by: ramneek
0 Replies

2. Programming

Object File Error

Hi, I've tried to compile a program I wrote with a Makefile, yet it returns an error: <<<test_log>>> itest_log.o /sr/local/bin/gcc -o test_log.o -I ../../../include -L ../../../lib -llog_mgr sh: itest_log.o: execute permission denied Error code 1 make: Fatal error: Command failed for... (3 Replies)
Discussion started by: Stevhp
3 Replies

3. Programming

mozilla object

hi this i tried for getting url form mozilla window. and also for getting mozilla object file. is there any plz tell the way. thanking u. ramesh (7 Replies)
Discussion started by: ramesh.jella
7 Replies

4. Shell Programming and Scripting

find string object

I have strings like "$AEUT_BIN/aeutut02.ksh $IFS_MACHINE $AEFA_CONTROL $MVS_CONTROL" I am looking for a way to find out how to obtain aeutut02.ksh from the above mentioned string in unix shell script. (1 Reply)
Discussion started by: phoenixV007
1 Replies

5. UNIX for Dummies Questions & Answers

Object reference not set to an instance of an object

I am new to PHP and UNIX. I am using Apache to do my testing on a Windows Vista machine. I am getting this error when I am trying to connect to a web service. I did a search and did not see any posts that pertain to this. Here is my function: <?php function TRECSend($a, $b, $c, $d,... (0 Replies)
Discussion started by: EddiRae
0 Replies

6. Programming

object creation

Hi, I was asked this question in interview.can you people please help me out in this. class A { int i; a() { i=10; cout << i; } } int main() { A a(); // what will be the program output } Thanks, Harika (3 Replies)
Discussion started by: harikamamidala
3 Replies

7. Programming

sizeof(object) in C++

Hi, I have defined the class and call the sizeof(object to class) to get the size. # include <iostream> # include <iomanip> using namespace std; class sample { private: int i; float j; char k; public: sample() { } (2 Replies)
Discussion started by: ramkrix
2 Replies

8. Red Hat

shared object

Hi, I would like to create a shared object ( .so). This shared object 1. uses the functions from a library. 2. Also it should be able to use the global variable in an app To achieve this what should I do ? 1) To use the functions in the library should I give the -ld option while... (1 Reply)
Discussion started by: rvan
1 Replies

9. Programming

passing object to function, columns class

I am working on a small columns class, since I use allot of tabular data. I am trying to set up code to allow me to efficiently read in tabular data, manipulate it, and write to output files. I more or less know what I need to do, but there are many options to sort through. I have the beginnings... (14 Replies)
Discussion started by: LMHmedchem
14 Replies

10. UNIX and Linux Applications

opends- help with custom object class

we have 2.2.0 of opends running on RedHat 2.6.21 and we're trying to setup a structure that will suit our needs. One of the things we'd like to do is create our own custom object classes based off some of the existing ones you get out of the box. The opends documentation covers this here (sorry, it... (1 Reply)
Discussion started by: snafu
1 Replies
std::slice_array< _Tp >(3)				     Library Functions Manual					std::slice_array< _Tp >(3)

NAME
std::slice_array< _Tp > - SYNOPSIS
Public Types typedef _Tp value_type Public Member Functions slice_array (const slice_array &) void operator%= (const valarray< _Tp > &) const template<class _Dom > void operator%= (const _Expr< _Dom, _Tp > &) const void operator&= (const valarray< _Tp > &) const template<class _Dom > void operator&= (const _Expr< _Dom, _Tp > &) const void operator*= (const valarray< _Tp > &) const template<class _Dom > void operator*= (const _Expr< _Dom, _Tp > &) const void operator+= (const valarray< _Tp > &) const template<class _Dom > void operator+= (const _Expr< _Dom, _Tp > &) const void operator-= (const valarray< _Tp > &) const template<class _Dom > void operator-= (const _Expr< _Dom, _Tp > &) const void operator/= (const valarray< _Tp > &) const template<class _Dom > void operator/= (const _Expr< _Dom, _Tp > &) const void operator<<= (const valarray< _Tp > &) const template<class _Dom > void operator<<= (const _Expr< _Dom, _Tp > &) const slice_array & operator= (const slice_array &) void operator= (const valarray< _Tp > &) const void operator= (const _Tp &) const template<class _Dom > void operator= (const _Expr< _Dom, _Tp > &) const void operator>>= (const valarray< _Tp > &) const template<class _Dom > void operator>>= (const _Expr< _Dom, _Tp > &) const void operator^= (const valarray< _Tp > &) const template<class _Dom > void operator^= (const _Expr< _Dom, _Tp > &) const void operator|= (const valarray< _Tp > &) const template<class _Dom > void operator|= (const _Expr< _Dom, _Tp > &) const Friends class valarray< _Tp > Detailed Description template<class _Tp>class std::slice_array< _Tp > Reference to one-dimensional subset of an array. A slice_array is a reference to the actual elements of an array specified by a slice. The way to get a slice_array is to call operator[](slice) on a valarray. The returned slice_array then permits carrying operations out on the referenced subset of elements in the original valarray. For example, operator+=(valarray) will add values to the subset of elements in the underlying valarray this slice_array refers to. Parameters: Tp Element type. Definition at line 80 of file valarray. Author Generated automatically by Doxygen for libstdc++ from the source code. libstdc++ Tue Jun 10 2014 std::slice_array< _Tp >(3)
All times are GMT -4. The time now is 03:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy