Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8048709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:33:55+00:00 2026-06-05T06:33:55+00:00

I have the following code : void testCopy(){ Balloon* b1=new Balloon(1,5,6,3); Balloon* b2=new Ballon(2,4,4,2);

  • 0

I have the following code :

void testCopy(){
   Balloon* b1=new Balloon(1,5,6,3);
   Balloon* b2=new Ballon(2,4,4,2);
   cpyBallon(b1,b2);
   assert(b1->getCenterx()==5);
   cout<<" cpyBalloon() OK!\n";
}

and the code for the cpyBalloon() is

void cpyBalloon(TCE& dest,TCE src){
    Balloon* b=(Balloon*) src;
    Balloon* d=new Balloon();
    *d=*b;
    dest=d;
}

where TCE if of type void*.
The problem is that i don’t know how to pass the object b2 which is of type Balloon* as void*&. I keep getting the following error:

    ..\src\/Domain/BalloonTest.h:68:18: error: invalid initialization of reference of type 'void*& {aka void*&}' from expression of type 'domain::Balloon*'
    ..\src\/Domain/Balloon.h:102:10: error: in passing argument 1 of 'void domain::delBalloon(void*&)'    

I know that b2 is the argument with which I have the problem but I don’t know how what to do to make it work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:33:57+00:00Added an answer on June 5, 2026 at 6:33 am

    As your code states, you have a Balloon class. You’ve dynamically allocated memory for two Balloon instances and called their constructors, filling them in with respective data. Then, you stored the address of each of those objects into proper raw pointers.

    This typedef:

    typedef void* TCE; // why?
    

    is pointless. A reference to a void* (void*&) is also pointless. Why? Because void* holds only one value – the first byte of a stream of bytes (their count is lost when you interpret anything as void*, that’s because count is type-dependent). So, it is already a pointer. You’re not passing an entire stream of bytes, just the address of the first byte. And then you further interpret it in the function as you’d like.

    But in this case, it’s not necessary to interpret it as such. A reference, when analyzed from a really low level, is just a pointer in disguise and enables us to reap the benefits of pointers safely (without thinking about it too much). There’s more to it, but for now, this is important.

    You can either:

    1. Create the instances on the stack (locally) and pass it in as a reference.
    2. Create the instances on the heap and acquire memory addresses, pass them in and process further.
    3. Or… You can take course of action 2 and cast them as void*, then undo the cast in the function (for whatever good that will do).

    Observe first:

        Balloon b1(1,5,6,3);
        Balloon b2(2,4,4,2);
    
        cpyBalloon(b1, b2);
    
        void cpyBalloon(Balloon& dest, const Balloon& src) 
        {
            dest = src; // invokes the assignment copy operator (imp. generated)
        }
    

    Observe second:

        Balloon* b1 = new Balloon(1,5,6,3);
        Balloon* b2 = new Balloon(2,4,4,2);
    
        cpyBalloon(b1, b2);
    
        void cpyBalloon(Balloon* dest, const Balloon* src) 
        {
            *dest = *src; // invokes the assignment copy operator (imp. generated)
        }
    

    Observe third (dear lord…):

        Balloon* b1 = new Balloon(1,5,6,3);
        Balloon* b2 = new Balloon(2,4,4,2);
    
        cpyBalloon((void*)b1, (void*)b2);
    
        void cpyBalloon(void* dest, const Balloon* src) 
        {
            *(Balloon*)dest = *(const Balloon*)src; // oh god...
        }
    

    You can also reinterpet_cast to circumvent all compiler help with type checking which is extremely dangerous and if you have to use it, you’re either very wrong or have a really nasty problem (which could stem from your approach being very wrong). Notice the constness around the source, that’s good practice whenever you want something copied and undamaged.

    Also note that there is a lot of memory management that arises in these situations, this is just a simple situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: void Processmethod() { QDialog *ProcessMessage = new QDialog; Ui::DialogProcessMessage
I have the following code: private void _DoValidate(object sender, DoWorkEventArgs e) { this.BeginInvoke(new MethodInvoker(()
i have following pseudo code : void siftup(int n) pre condition n>0 && heap(1,n-1)
I have the following code: void cStatisticDBSerializer::GetStats (std::map <std::string, long long >& ioCounterStats, std::map
Suppose I have the following code: void* my_alloc (size_t size) { return new char
SO I have the following code public void rand(int N){ double[]x=new double[N]; double[]y=new double[N];
I have the following code void foo() { char* pcBlock = new char[1000]; ...
I have the following code: void testConfirmBookNotFound() { def book = new Book(id: 1,
I have following code: private void ProcessQueue() { foreach (MessageQueueItem item in GetNextQueuedItem()) PerformAction(item);
I have the following code: void doPlayerMove(void) { bool moved = false; while (!moved)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.