CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 40
  1. #1
    Join Date
    Jul 2001
    Posts
    76

    why i can't assign I[5] ?

    #include <iostream.h>
    class A{
    private:
    int x,y ;
    public:
    A(int a=0,int b=0){y=b;x=a;}
    void init(int a,int b){x=a;y=b;}
    void show(){cout<<x<<" "<<y<<endl;}
    };
    void main()
    {
    A l[5]={(1,1),(2,2),(3,3),(4,4),(5,5)};
    A *p;
    p=l;
    l[3].init(8,3);
    p->init(3,4);
    for(int i=0;i<5;i++)
    (*(p+i)).show();
    }

    when i execute this code,I[0] was shown (1.0),but I assign it (1,1),i[0] was not assigned.so was i[1],i[2]...

    What can i do to assign i[5]?thanks very much.

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    Index count begins with 0. That is a fundamental concept of computer architecture.

    If you want to start with array[1], array[2], etc. then you should ignore array[0]. In your example, create six arrays.

    Kuphryn

  3. #3
    Join Date
    Jul 2001
    Posts
    76
    In my program ,i assign array like this:

    A l[5]={(1,1),(2,2),(3,3),(4,4),(5,5)};

    but it execute like this :

    1 0
    2 0
    3 0
    4 0
    5 0

    it like:
    y of class A is not assigned.but x of class A is assigned.

    why?thank you .What can i do to assign y of class A?

  4. #4
    Join Date
    Feb 2002
    Posts
    5,757
    One solution is a loop.

    Code:
    for (unsigned int i = 0; i < 5; ++i)
    I[i].Create(i + i, i + i);
    Kuphryn
    Last edited by kuphryn; October 28th, 2002 at 12:51 AM.

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Try this:
    Code:
    A l[5]={A(1,1), A(2,2), A(3,3), A(4,4), A(5,5)};
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Oct 2002
    Posts
    36
    Although it ugly, there is a way to use Kuphryn's loop and Graham's constructor method in one.

    This code relies on operator new[] that's provided in the <new> header and then using placement new to initialise each object's construction in a loop. Unfortunately it looks nasty, it's not easy to see what's going on, and you nust control the destruction and memory freeing yourself

    Code:
    #include <new>
    #include <iostream>
    
    
    class A{
    
    	int x,y ;
    public:
    	A(int a=0,int b=0){y=b;x=a;}
    	void init(int a,int b){x=a;y=b;}
    	void show(){std::cout << x << " " << y << std::endl;}
    };
    
    int main(){
    
    	const int nArrSz = 5;
    	
    	A *l = static_cast<A*>(operator new[] (nArrSz * sizeof(A)));//Allocate raw memory
    	if(!l){
    		std::cout << "Error" << std::endl;
    		return 1;
    	}	
    	
    	for(int i = 0;i < nArrSz;i++)
    		new (l+i) A(i+1,i+1);//Call constructor for each object in array
    	
    	
    	A *p;
    	p=l;
    	l[3].init(8,3);
    	p->init(3,4);
    	
    	for(int i=0;i<5;i++)
    		(*(p+i)).show();
    		
    	for(int i = 0;i < nArrSz;i++)
    		l[i].~A();//Call destuctor for each object in array
    		
    	operator delete[] (l);//free memory	
    		
    }
    Enjoy!

  7. #7
    Join Date
    Jul 2002
    Location
    American Continent
    Posts
    340

    Solution exploiting struct-like behaviour

    [Y: Solution exploiting struct-like behaviour]

    What the OP needs to do is:
    1.Change the "private" to public. If you need to initialize the class member from an array, it needs to be accessible so you can't hide with "private".
    2.Remove any constructor you declared. The compiler will decide how to initialize the object, not you.
    3.Change the round bracket in your parameter list to curly bracket.

    This is how the working code looks like:
    Code:
    //...
    class A
    {
    public:
    	int x;
    	int y;
    public:
    	void init(int a,int b){x=a;y=b;}
    	void show() {printf("%d %d\r\n", x, y);}
    };
    
    int main(int argc, char* argv[])
    {
    	A l[5] = {{1,1},{2,2},{3,3},{4,4},{5,5}};
    //...
    }
    [Yves : cleaned up the post a bit]
    Last edited by Yves M; October 29th, 2002 at 07:03 AM.

  8. #8
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Originally posted by AnthonyMai
    I just don't understand why the OP posted something so simple and straightforward and no one can give a correct and straight answer?
    I didn't find a problem with some of the answers here; maybe
    they didn't want to totally alter his class design. What was wrong
    with Graham's response?

    What the OP needs to do is:
    1.Change the "private" to public. If you need to initialize the class member from an array, it needs to be accessible so you can't hide with "private".
    ... and what if the OP wants to use the information-hiding ideas
    inherent in C++? It can be difficult to make an abstract data
    type of others can get access to your private members.
    2.Remove any constructor you declared. The compiler will decide how to initialize the object, not you.
    ... and what if the OP wants to provide some other form of
    initialization in this class? I don't think he should be forced to
    use inheritance to create such a simplistic class.

    Just as an aside, just because you can change a bunch of things
    in order to "implement a solution", it doesn't mean it's an ideal
    solution. Frankly, I wouldn't want to compromise my class design
    so radically like you suggest; I'd rather use any number of
    alternative methods.

    --Paul

  9. #9
    Join Date
    Oct 2002
    Posts
    36
    Originally posted by AnthonyMai
    I just don't understand why the OP posted something so simple and straightforward and no one can give a correct and straight answer?
    As I see it, there is no correct & straight answer. Your idea may indeed be closest to how the OP was attempting this, but it involves him changing how the class operates and the privilege levels of the data members, so therefore its not the definitive answer.

    Only the OP can decide which of the methods presented fits closest to what he/ she wants to do

  10. #10
    Join Date
    Oct 2002
    Posts
    36
    Apologies to previous poster. Didnt see your response when I began mine.

  11. #11
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Originally posted by AnthonyMai
    I just don't understand why the OP posted something so simple and straightforward and no one can give a correct and straight answer?
    Point out the error in my answer, please.
    What the OP needs to do is:
    1.Change the "private" to public. If you need to initialize the class member from an array, it needs to be accessible so you can't hide with "private".
    So he has to violate encapsulation and expose the members of his class to random modification?
    2.Remove any constructor you declared. The compiler will decide how to initialize the object, not you.
    Tosh.
    3.Change the round bracket in your parameter list to curly bracket.

    This is how the working code looks like:
    Code:
    //...
    class A
    {
    public:
    	int x;
    	int y;
    public:
    	void init(int a,int b){x=a;y=b;}
    	void show() {printf("%d %d\r\n", x, y);}
    };
    
    int main(int argc, char* argv[])
    {
    	A l[5] = {{1,1},{2,2},{3,3},{4,4},{5,5}};
    //...
    }
    So why didn't you also change the "class" to "struct" and remove the access specifiers and member functions, since yours is a "C" answer, not a "C++" answer?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  12. #12
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Originally posted by dumah
    Apologies to previous poster. Didnt see your response when I began mine.
    Oh don't worry about it If you take a couple of minutes to type
    our your reply, I could have hit "Submit" in that time. The more
    correct responses the merrier.

    --Paul

  13. #13
    Join Date
    Oct 2002
    Posts
    36
    Originally posted by PaulWendt


    Oh don't worry about it If you take a couple of minutes to type
    our your reply, I could have hit "Submit" in that time. The more
    correct responses the merrier.

    --Paul
    LOL - Yeah, looking at it I was really slow typing that out - work can be such a distracting place when you are trying to post on forums

  14. #14
    Join Date
    Jul 2002
    Location
    American Continent
    Posts
    340
    Originally posted by AnthonyMai
    I just don't understand why the OP posted something so simple and straightforward and no one can give a correct AND straight answer?

    Graham said:
    Point out the error in my answer, please.
    Graham, you did not give an answer that is correct AND straight. By "straight" I mean it should be concise and elegant. You solution calls the constuctor individually. Obviously if there are 1000 elements in the array you would call constructor and destructor 100 times respectively, if there are 10000 you will call 10000 times. So it is not straight at all.

    So why didn't you also change the "class" to "struct" and remove the access specifiers and member functions, since yours is a "C" answer, not a "C++" answer?
    With due respect I do NOT think you can write a struct with member function in C. Structs with member functions is a feature available in C++ but not C.

  15. #15
    Join Date
    Jul 2002
    Location
    American Continent
    Posts
    340
    ... and what if the OP wants to provide some other form of
    initialization in this class? I don't think he should be forced to
    use inheritance to create such a simplistic class.
    "Some other form of initialization" is construct first and then initialize.

    It is indeed a very simplistic class, the very fact that it can be initialized using some POD data array tells you it is a very C like simplistic class.

    Just as an aside, just because you can change a bunch of things
    in order to "implement a solution", it doesn't mean it's an ideal
    solution. Frankly, I wouldn't want to compromise my class design
    so radically like you suggest; I'd rather use any number of
    alternative methods.
    Since it is a simple class, please quit the talk of class design, and proper data encapsulation, and a bunch of other high level OOP concepts. These concepts may be good when the design is complicated. But they are not important here when things are rather simple and can be done in the simplest way.

    [Yves : small edits]
    Last edited by Yves M; October 29th, 2002 at 07:07 AM.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured