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

  • Home
  • SEARCH
  • 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 7591349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:37:49+00:00 2026-05-30T20:37:49+00:00

This is my sample program #include stdafx.h class B { public: int i,j; };

  • 0

This is my sample program

#include "stdafx.h"

class B
{
public:
    int i,j;
};

class A
{
public:
    B b[2];
    A()
    {
        b[0].i = 1;
        b[0].j = 2;
        b[1].i = 3;
        b[1].j = 4;
    }
    B* function()
    {
        return b;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    A a;
    B* obj = new B();
    obj = a.function();
    return 0;
}

I have to get the array of b objects(ie, need all the values, b[0].i, b[0].j,b[1].i and b[1].j)

But when I tried it with this code, only one object is returned.

  • 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-05-30T20:37:50+00:00Added an answer on May 30, 2026 at 8:37 pm

    What you state in the question is not true. Two objects are indeed returned. Access them with obj[0] and obj[1].

    I guess you are looking at obj under the debugger and the IDE cannot know that you mean for your pointer obj to be an array of two objects. So the tooltips will only show the first object, obj[0], or *obj. But the other object, obj[1] is definitely there.

    Add the following line after the call to a.function:

    printf("%d, %d, %d, %d\n", obj[0].i, obj[0].j, obj[1].i, obj[1].j);
    

    and you will see this output:

    1, 2, 3, 4
    

    Note that there is no point in the line B* obj = new B(); since you immediately overwrite obj. You should do it this way:

    B* obj = a.function();
    

    Your code is also a little dangerous in that you must keep a alive at least as long as you are making references to obj.

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

Sidebar

Related Questions

Imagine this sample java class: class A { void addListener(Listener obj); void removeListener(Listener obj);
Consider this sample code: <div class=containter id=ControlGroupDiv> <input onbeforeupdate=alert('bingo 0'); return false; onclick=alert('click 0');return
This is sample program from Beginning Linux Programming book: #include <stdio.h> #include <term.h> #include
I have written a sample program : #include<iostream> #include<set> #include<conio.h> using namespace std; int
I found this sample code on the msdn library #include <iostream> int main() {
i have written a sample program.below: #include<iostream> #include<string> #include<set> using namespace std; int main()
Suppose I have the following program: #include <stdio.h> int main() { printf(This is a
I'm having trouble getting a sample program to link correctly (in this case against
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Consider this sample class, class TargetClass { private static String SENSITIVE_DATA = sw0rdfish; private

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.