Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Who can know the question of this program

Who can know the question of this program

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelp
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    richardye
    wrote on last edited by
    #1

    I define a class CArray2D,inside it I imeplement a "proxy" class CArray1D, so that if I create a object of CArray2D like CArray2D array(2,3), I can output the its element like cout<<array[0][0]. But when I run it, I met with serials of strange questions like error address. Can anyone tell me why? Many thanks The code is as follows class CArray2D { public: void Print() { CArray1D *pHead = pArray1D; for(int i=0; i<size2D; i++) { cout<<"[ "<<i<<" }"; pHead->Print1D(); pHead = pHead+i*sizeof(CArray1D); cout<<endl; } } CArray2D(int n2DWidth, int n1DWidth):size2D(n2DWidth) { pArray1D = new CArray1D[size2D]; }; ~CArray2D() { delete[] pArray1D; }; class CArray1D { public: CArray1D(int n1DWidth=3):size1D(n1DWidth) { pArray = new int[size1D]; for(int i=0; i<size1D; i++) { *(pArray+i*sizeof(int)) = 0; } }; ~CArray1D() { delete[] pArray; }; const int& operator[](int nIndex) const { return *(pArray+nIndex*sizeof(int)); } int& operator[](int nIndex) { return *(pArray+nIndex*sizeof(int)); } void Print1D() { for(int i=0; i<size1D; i++) { cout<<"["<<i<<"]="<<*(pArray+i*sizeof(int))<<" "; } } private: int size1D; int *pArray; }; const CArray1D& operator[](int nIndex) const { return *(pArray1D+nIndex*sizeof(CArray1D)); }; CArray1D& operator[](int nIndex) { return *(pArray1D+nIndex*sizeof(CArray1D)); }; public: int size2D; CArray1D *pArray1D; }; int main() { CArray2D array(2,3); array.Print(); array[0][0] = 3; array.Print(); cout<<array[0][0]<<endl; return 0; } } Tomorrow is another day

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups