随笔:18 文章:0 评论:0 引用:0
C++博客 首页 发新随笔
发新文章 联系 聚合管理

当有一些已经被分配但是尚未处理的(raw)内存,可以在这些内存中构造
一个对象。使用的是一个特殊的 operator new,它被称为 placement new。
使用 place new 的例子,

class Widget
{
 public:
  Widget(int widgetSize);
  ...
};

Widget *constructWidgetInBuffer(void *buffer, int widgetSize)
{
  return new (buffer) Widget(widgetSize);
}

当程序使用共享内存或 memory-mapped I/O 时这个函数可能有用,因为
在这样的程序里对象必须被放置在一个确定地址上或一块被例程分配的
内存里。

返回的表达式是,
new (buffer) Widget(widgetSize)
它是 new 操作符的一个用法,需要使用一个额外的变量(buffer),
当 new 操作符隐含调用 operator new 函数时,把这个变量传递给它。被调用的
operator new 函数除了带有强制的参数 size_t 外,还必须接受 void* 指针参数,
指向构造对象占用的内存空间。 这个 operator new 就是 placement new, 它看
上去象这样,
void * operator new(size_t, void * location)
{
   return location;
}
这可能显得有些简单,这是这就是 placement new 需要做的事情。毕竟 operator new
的目的是为对象分配内存然后返回指向该内存的指针。
posted @ 2008-09-30 14:39 王者归来 阅读(465) | 评论 (0)编辑 收藏
 

1. static const int m_ nMyConst; //只有静态常量整型(枚举型)数据成员在可以在类中初始化; (enum MyEnum{A, B, C};
 static const MyEnum m_myenum = B;
 )
const int m_nMyVal; //必须在构造函数基/成员初始值设定项列表中初始化。

2. 关于继承
#include "stdafx.h"
#include <iostream>
using namespace std;

class student
{
public:  
  student(char *pName = "No name")  
  {
 strcpy((char*)name, pName);
 semesterhours = 0;
  } 
protected:
  void addcourse(int hours)  
  {
 semesterhours += hours;
 cout << "semesterhours" << semesterhours << endl;
  }
public:
 int pubval;
protected:  
 int proval;
//private:
  char  name[40];  
  int  semesterhours;  
private:
 int prival;
};

class graduatestudent:protected   student  
{
public:
 void addcoursetoo(int h)
 {
  pubval = 10;
  proval = 20;
  //prival = 30;
  prival2 = 40;
  student::addcourse(h);
 }
private:
 int prival2;
}; 

class citizen:private graduatestudent
{
public:
 void addcourse3(int h)
 {
  addcoursetoo(100);
  addcourse(400);
  pubval = 200;
  proval = 300;
 }
};

int _tmain(int argc, _TCHAR* argv[])
{
 student   ds("undergradute");  
   graduatestudent   gs;  
 //gs.addcourse(30);  
 gs.addcoursetoo(40);

 getchar();

 return 0;
}

 3. 定义只能创建一个实例的类
#include "stdafx.h"
#include <windows.h>
#include <iostream>

#include <iostream>
using namespace std;

class   CDebug  
  {  
  public:  
   ~CDebug(){} 
   
          void   Print(const char *str)
    {
     printf(str);
    }
          static   CDebug &GetInstance()    
          {  
    static CDebug debug;
                 //return   s_debug;    
    return debug;
    
          }
    void SetName(char * n)
    {
     strcpy(Name, n);
    }
    void PrintName(void)
    {
     printf("Name: %s\n", Name);
    }
  private:  
          HANDLE   m_StdOut; 
    char Name[10];

              static    CDebug  * s_debug;    
          //防止直接生成实例  
    CDebug(){}  
  };  


int _tmain(int argc, _TCHAR* argv[])
{
 //CDebug object = CDebug::GetInstance();
 CDebug::GetInstance().Print("hello dshe\n");
 CDebug::GetInstance().SetName("ASON");
 CDebug::GetInstance().PrintName();

 CDebug object2 = CDebug::GetInstance();
 object2.PrintName();
 getchar();

 return 0;
}

 4.
#include "stdafx.h"
#include "iostream"
using namespace std;

class MyClass
{
public:
 MyClass(char * n = "No Name")
 {
  printf("MyClass\n");
  strcpy(m_Name, n);
 }
 static MyClass GetObj(char * nn)
 {
  return MyClass(nn);
 }
 ~MyClass()
 {
  printf("~MyClass\n");
 }
 void Print(void)
 {
  printf("Name: %s\n", m_Name);
 }
private:
 char m_Name[10];
};

int _tmain(int argc, _TCHAR* argv[])
{
 MyClass p;
 p = MyClass("aAAbBB");
 p.Print();
 getchar();

 return 0;

posted @ 2008-09-11 16:06 王者归来 阅读(333) | 评论 (0)编辑 收藏
 
1.
char * const p;//常量指针,p的值不可以修改
char const *p;//指向常量的指针,指向的常量值不可以改
const char *p;//和char const *p

2.
char (*str)[20];//str是一个数组指针,即指向数组的指针
char *str[20];//str是一个指针数组,其元素为指针型数据

3.
用变量a给出下面的定义

a)
一个整型数(An integer
b)
一个指向整型数的指针(A pointer to an integer

c)
一个指向指针的的指针,它指向的指针是指向一个整型数(A pointer to a pointer to an integer

d)
一个有10个整型数的数组(An array of 10 integers

e)
一个有10个指针的数组,该指针是指向一个整型数的(An array of 10 pointers to integers

f)
一个指向有10个整型数数组的指针(A pointer to an array of 10 integers

g)
一个指向函数的指针,该函数有一个整型参数并返回一个整型数(A pointer to a function that takes an integer as an argument and returns an integer

h)
一个有10个指针的数组,该指针指向一个函数,该函数有一个整型参数并返回一个整型数( An array of ten pointers to functions that take an integer argument and return an integer 答案是:

a) int a; // An integer
b) int *a; // A pointer to an integer
c) int **a; // A pointer to a pointer to an integer
d) int a[10]; // An array of 10 integers
e) int *a[10]; // An array of 10 pointers to integers
f) int (*a)[10]; // A pointer to an array of 10 integers
g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer
h)
int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer
posted @ 2008-09-07 19:41 王者归来 阅读(1504) | 评论 (0)编辑 收藏
 

<html>
<head>
<script type='text/javascript'><!--
function cellImg(idCell, imgName) {
document.getElementById(idCell).style.background = "url(" + imgName + ")";
}
//--></script>
</head>
<body>
<table width="50%" height="50%" border="1">
<tr>
<td id='c1'>
<a href="javascript:cellImg('c1','red.gif')">c1</a>
</td>
<td id='c2'>
<a href="javascript:cellImg('c2','blue.gif')">c2</a>
</td>
</tr>
</table>
</body>
</html>

posted @ 2008-08-28 17:02 王者归来 阅读(251) | 评论 (0)编辑 收藏
 

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

<window id="HideShowTesting" title="Hide/Show Testing" width="300" height="200"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script>
function checkPositions()
{
  // Get references to the hidden and collapsed labels
  var hidden=document.getElementById("hiddenLabel");
  var collapsed=document.getElementById("collapsedLabel");

  // Get the size of the hidden label using the width and height properties
  // of the boxObject. Use alert() to dispaly the size.
  var val="Size of hidden element: "+ hidden.boxObject.width+","+hidden.boxObject.height;
  alert(val);

  // Remove the hidden attribute so that the label appears.
  hidden.removeAttribute("hidden");

  // Get the size of the collapsed label.
  val="Size of collapsed element: "+ collapsed.boxObject.width+","+collapsed.boxObject.height; 
  alert(val);

  // Remove the collapsed attribute so that the label appears.
  collapsed.removeAttribute("collapsed");
  // or collapsed.collapsed = false;

  val="Size of collapsed element: "+ collapsed.boxObject.width+","+collapsed.boxObject.height;
  alert("again: " + val);
}
</script>

<button label="Check Positions" oncommand="checkPositions();"/>
<hbox>
<label id="hiddenLabel" hidden="true" value="aaa" style="background-color: #FFFF00;"/>
<label id="collapsedLabel" collapsed="true" value="bbbccc" style="background-color: #00FFFF;"/>
</hbox>
</window>

posted @ 2008-08-28 13:32 王者归来 阅读(285) | 评论 (0)编辑 收藏
 

An smpersand (&) may be escaped numerically (&) or with a general entity (&amp;).

posted @ 2008-08-22 17:56 王者归来 阅读(182) | 评论 (0)编辑 收藏
 
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe
posted @ 2008-08-18 21:46 王者归来 阅读(212) | 评论 (0)编辑 收藏
 
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe
posted @ 2008-08-18 21:46 王者归来 阅读(189) | 评论 (0)编辑 收藏
 
A.
set homeDir to (path to home folder) as string
set posixHomeDir to POSIX path of homeDir
display dialog posixHomeDir
set posixFileName to posixHomeDir & "Library/Application Support/Firefox/profiles.ini"
display dialog posixFileName
set appleFileName to POSIX file posixFileName
set fileContent to (read appleFileName)
display dialog fileContent
posted @ 2008-08-06 16:16 王者归来 阅读(247) | 评论 (0)编辑 收藏
 
var obj = new Object();
var propName = "put string here" + theremybeavariable;
obj[propName] = ......
posted @ 2008-07-29 16:42 王者归来 阅读(359) | 评论 (0)编辑 收藏
仅列出标题
共2页: 1 2 
CALENDER
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(1)

随笔档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜


Powered By: 博客园
模板提供沪江博客