本文介绍了获取qlist大小作为结构大小C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我怎样才能获得像sizeof struct这样的自己列表的大小。我知道sizeof是如何工作的。

Hello, How can i get the size of my own list like sizeof struct. I know how sizeof works for example.

enum NETCDF_Type
	{
		NETCDF_NONE,      //!< unknown data foramt
		NETCDF_STRING,
		NETCDF_UCHAR,		//!< signed 1 byte integer
		NETCDF_CHAR,		//!< signed 1 byte integer
		NETCDF_SHORT, 	//!< signed 2 byte integer
		NETCDF_INT,		//!< signed 4 byte integer
		NETCDF_USHORT,	//!< unsigned 2-byte int
		NETCDF_UINT,		//!< unsigned 4-byte int
		NETCDF_FLOAT32,	//!< signed 4-byte int
		NETCDF_DOUBLE64,	//!< signed 8-byte int
		NETCDF_LONGLONG,
		NETCDF_ULONGLONG
	};

void getSize( const QList< NETCDF_Type > myList )
{
     for( int i = 0; i < myList.count(); i++ )
     {
          //do something
          //I need a size of which NETCDF_Type i am using.
          //Without sizeof();
     }
}





我尝试过:





What I have tried:

struct MyStruct
{
     char v1;
     char v2;
     char v3;
     short v4;
};

int mSize = sizeof( MyStruct ); mSize => 6;

struct MyStruct
{
     char v1;
     char v2;
     char v3;
     char v4;
     short v;
};


int mSize = sizeof( MyStruct ); mSize => 6;

推荐答案

Quote:

//我需要一个我正在使用的NETCDF_Type的大小。

//没有sizeof();

//I need a size of which NETCDF_Type i am using.
//Without sizeof();

我会为此编写一个方法,例如

I would write a method for that, e.g.

size_t getSize(NETCDF_Type typ);



方法实现由您直接决定,使用开关

[/ update]


The method implementation is up to you, in a straightforward approach, use a switch.
[/update]


这篇关于获取qlist大小作为结构大小C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 02:59
查看更多