The structure does not apply to an object-oriented program

Or that?

If an object-oriented design uses a language construct that provides element data by default, if there is an equally useful construct that properly hides data elements?

EDIT: One of the respondents mentioned that if there is no invariant, you can use a structure. This is an interesting observation: structure is a data structure, i.e. Contains related data. If the data members in the structure are interconnected, is there always an invariant?

+5
source share
17 answers

++ struct class , / . ( .)

" " " ". . . - ( ), ; , , .

+18

. get/set , / , (, , ).

, , setter , - , , , .. , , ( , )

, , - . , , .

, "Point", , "" "" , .

+13

#, , structs "left-as-values":

public struct Point
{
    int X;
    int Y;
}

P/Invoke , , .

? , , , . , , , .

+4

++ (.. public struct private ). , , C.

, , .

( ), , . :

struct CPoint
{
   int x ;
   int y ;

   CPoint() : x(0), y(0) {}

   int getDistanceFromOrigin() const
   {
      return std::sqrt(x * x + y * y) ;
   }
} ;

inline CPoint operator + (const CPoint & lhs, const CPoint & rhs)
{
   CPoint r(lhs) ;
   r.x += rhs.x ;
   r.y += rhs.y ;
   return r ;
}

x CPoint, - CPoint.

, , , ++ ( ) , , , .

( ), , . :

class CString
{
   public :
      CString(const char * p) { /* etc. */ } ;
      CString(const CString & p) { /* etc. */ } ;

      const char *     getString() const { return this->m_pString ; }
      size_t           getSize() const { return this->m_iSize ; }

      void             copy { /* code for string copy */ }
      void             concat { /* code for string concatenation */ }

   private :
      size_t           m_iSize ;
      char *           m_pString ;
} ;

inline CString operator + (const CString & lhs, const CString & rhs)
{
   CString r(lhs) ;
   r.concat(rhs) ;
   return r ;
}

, concat ( ), . .

, , .

- .

, , , (, ).

, , , .

, , # (.. ). , .

+4

( ).

.

, , , .

, , .

, , - C, .

+2

. class , .

struct , public. , std::pair<T, U> ++.

?

. Point, x and y >= 0. ,

/* x >= 0 && y >= 0 for this classes' objects. */

, x y, . , , , , , : .

+2

, .

public struct Point {
    int x;
    int y;
}

:

public class Point {
    private int x;
    private int y;
    public void setX(int x) { this.x=x; }
    public int getX(); { return x; }
    public void setY(int y) { this.y=y; }
    public int getY(); { return y; }
}

, , x y. , - .

+1

. -.

0

, ++ , . , . .

0

, . , . , . ; , , , , COM- .

0

- , . .

- , . - .

0

, , - , (.. ). .

, -, , , , . , , . , ?

0

, C ++, , # ( .Net), , , , ... , .

0

, . .

0

:

vs struct ++?

struct class ++

Stroustrup ++:

, . struct , . , " , ".

0

, , , , . , , Point : ,

Point foo;
//...
foo.x = bar;

, , ,

#define X 0
#define Y 1
//...
foo[X] = bar;

. , [0.0..1.0]; , [0..1023]. , - foo.x = 1023, x < 1.0?

, ++ structs for Points, - , - 20 , ++ - , foo.setX(1023) foo.x = 1023. .

0

The structures are beautiful as long as they remain small. As you probably know, they are allocated on the stack (and not heap), so you need to look at the size. They can come in handy for small data structures such as Point, Size, etc. Usually a class is the best choice, being a reference type and all.

-1
source

All Articles