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
IS_A(3) 								 1								   IS_A(3)

is_a - Checks if the object is of this class or has this class as one of its parents

SYNOPSIS
bool is_a FALSE (object $object, string $class_name, [bool $allow_string]) DESCRIPTION
Checks if the given $object is of this class or has this class as one of its parents. PARAMETERS
o $object - The tested object o $class_name - The class name o $allow_string - If this parameter set to FALSE, string class name as $object is not allowed. This also prevents from calling autoloader if the class doesn't exist. RETURN VALUES
Returns TRUE if the object is of this class or has this class as one of its parents, FALSE otherwise. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.3.9 | | | | | | | Added $allow_string parameter | | | | | 5.3.0 | | | | | | | This function is no longer deprecated, and will | | | therefore no longer throw E_STRICT warnings. | | | | | 5.0.0 | | | | | | | This function became deprecated in favour of the | | | instanceof operator. Calling this function will | | | result in an E_STRICT warning. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 is_a(3) example <?php // define a class class WidgetFactory { var $oink = 'moo'; } // create a new object $WF = new WidgetFactory(); if (is_a($WF, 'WidgetFactory')) { echo "yes, $WF is still a WidgetFactory "; } ?> Example #2 Using the instanceof operator in PHP 5 <?php if ($WF instanceof WidgetFactory) { echo 'Yes, $WF is a WidgetFactory'; } ?> SEE ALSO
get_class(3), get_parent_class(3), is_subclass_of(3). PHP Documentation Group IS_A(3)
All times are GMT -4. The time now is 05:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy