GVKun编程网logo

struct初始化及构造函数与class区别(struct默认构造函数)

16

对于想了解struct初始化及构造函数与class区别的读者,本文将提供新的信息,我们将详细介绍struct默认构造函数,并且为您提供关于15-struct(构造函数,重载)、@PostConstru

对于想了解struct初始化及构造函数与class区别的读者,本文将提供新的信息,我们将详细介绍struct默认构造函数,并且为您提供关于15-struct(构造函数,重载)、@PostConstruct、@Autowired 以及构造函数的执行顺序、@postconstruct初始化的操作(转载)、C ++中的Struct构造函数?的有价值信息。

本文目录一览:

struct初始化及构造函数与class区别(struct默认构造函数)

struct初始化及构造函数与class区别(struct默认构造函数)

遇到一个bug,debug与release的表现不同。经查是struct结构的默认赋值在debug与release是不同的。实际上包括class及基本类型,未手动初始化的话,在debug与release下是不同的,因为release的一些优化可能会导致默认赋值时,为随机的内存位置,其值不被重写。居然一直没遇到struct的这个问题,难道我以前都是手动初始化的吗- -

随便把strcut的初始化和默认构造函数等问题查了下。转贴如下。

结构体实例(包括共用体)和类实例的初始化方法完全相同,二者都可以应用于继承层次中。不同点是结构体(包括共用体)默认成员为public,而类默认成员是private型的。

一、若类和结构体所有数据成员均为public型,可采取如下带花括号形式进行初始化。

    注意:

①        不论值的个数多少,都必须使用花括号定界

②        未指定值的数据成员编译器会自动初始化为默认值

③        这种初始化对象方式,要求所有数据成员必须为public型

④        这种初始化对象方式,要求类中不能编写任何构造函数

struct S {  //class S 效果一样

    int            x;

    unsigned short y;

};

S testS1={100,123};

S testS2={200};//未指定值的数据成员初始化为默认值,这里os2.y=0;

S TestS[4]={ {100,10},

             {200,20},

             {300} };//未指定值的初始化为默认值,os[2].y,os[3].x,os[3].y。

二、若数据成员有private或protected型,或是提供了构造函数,必须使用构造函数来进行初始化。

struct S { //class S可自行试验,结果相同

    private:

        int x;

    public:

        double y;

        S(void){}

        S(int idemo,double ddemo) {x=idemo;y=ddemo;}

        void show(void) {cout<<x<<''''\t''''<<y<<endl;}

};

S os1;//将调用默认构造函数(无参构造函数)

S os2(1000,2.345);

S os3=S(2000,4.567);

S os[4]={S(10,1.234),S(20,2.234)};//未初始化的将调用默认构造函数。如此时没有默认构造函数会出错。

重要提示:

①        在S os3=S(2000,4.567);语句中,因是声明并初始化os3对象,所以将调用S(int,double)构造函数对os3进行初始化。

②        S os3(2000,4.567); 等价于 S os3=S(2000,4.567);

③        但如果os3已经存在了,S os3(100,1.234);os3=S(2000,4.567),则表示用一个临时对象赋值给os3,将调用operator=,然后系统再释放这个临时产生的对象。系统默认的=运算是将源对象的数据成员的值复制到目标对象中的数据成员中。

三、接受一个参数的构造函数允许使用赋值句法初始化对象。

说明代码如下:

#include <iostream>

using namespace std;

class C {

    private:

        int x;

    public:

        C(int idemo) {x=idemo;}

        void show(void) {cout<<x<<endl;}

};

struct S {

    private:

        int x;

    public:

        S(int idemo) {x=idemo;}

        void show(void) {cout<<x<<endl;}

};

int main(int argc, char *argv[]){

    C oc=1000;//不能企图加花括号

    oc.show();

    S os=2000;//不能企图加花括号

    os.show();

    return EXIT_SUCCESS;

}


15-struct(构造函数,重载)

15-struct(构造函数,重载)

 

必须充分掌握struct的使用,包括其构造和重载函数的写法:

#include <iostream>
using namespace std;

struct node {
	int x, y;
	node(){ 
		
	}
//	node (int x, int y){
//		this->x = x;
//		this->y = y;
//	}
	node (int x, int y) : x(x), y(y){
		
	}
	node operator + (const node s){
		node rt;
		rt.x = this->x + s.x;
		rt.y = this->y + s.y;
		return rt;
	}
	node operator * (const node s){
		node rt;
		rt.x = this->x * s.x;
		rt.y = this->y * s.y;
		return rt;
	}
	bool operator ==(const node s){
		if(s.x == this->x && s.y == this->y){
			return 1;
		}
		else{
			return 0;
		}
	}
	bool operator < (const node s){
		if(this->x < s.x){
			return 1;
		} 
		else{
			return 0;
		}
	}
}; 

int main(){
	node a, b;
	a.x = a.y = 1;
	b.x = b.y = 3;
	a = a + b;
	cout << "a: " << a.x  << " " << a.y << endl;
	node c(1, 3);
	cout << "c: " << c.x << " " << c.y << endl;
	node d = c;
	cout << "d: " << d.x << " " << d.y << endl;
	if(d == c){
		cout << "d = c" << endl;
	}
	if(a < c){
		cout << "a < c" << endl;
	} 
	else{
		cout << "a !< c" << endl;
	}
	return 0;
}

  

@PostConstruct、@Autowired 以及构造函数的执行顺序

@PostConstruct、@Autowired 以及构造函数的执行顺序

结论先行:构造函数 -> PostConstruct -> @Autowired 依次执行

由于项目需要启动时加载一个配置信息,所以想到了用 @PostConstruct,如下所示:

@Configuration
public class BUserCenterConfig {

    @Value("${b.user.url}")
    public String userCenterUrl;

    @Value("${b.user.appId}")
    public String userCenterAppId;

    @PostConstruct
    public void setDefaultConfig() {
        UserCenterConfig.SetInterfaceUrl(userCenterUrl);
        UserCenterConfig.SetDefaultAppId(userCenterAppId);
    }
}

BeanTest.java

@Service
public class BeanTest {

    @Autowired
    private BeanTest2 beanTest2;

    public BeanTest() {
        System.out.println("this is BeanTest construct. ");
    }

    @PostConstruct
    private void init() {
        System.out.println("this is BeanTest init function. ");
        beanTest2.test2();
    }
}

BeanTest2.java

@Service
public class BeanTest2 {

    @PostConstruct
    private void init() {
        System.out.println("this is BeanTest2 init function. ");
    }

    public BeanTest2() {
        System.out.println("this is BeanTest2 construct. ");
    }

    void test2() {
        System.out.println("this is BeanTest2 test2 function. ");
    }
}

启动项目,输出结果如下:

this is BeanTest construct. 
this is BeanTest2 construct. 
this is BeanTest2 init function. 
this is BeanTest init function. 
this is BeanTest2 test2 function.

 

@postconstruct初始化的操作(转载)

@postconstruct初始化的操作(转载)

原文地址:https://www.cnblogs.com/qingruihappy/p/7861623.html

 

从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion);@PostConstruct和@PreDestroy。这两个注解被用来修饰一个非静态的void()方法 。写法有如下两种方式:

@PostConstruct

Public void someMethod() {}
                                                                                    
或者

public @PostConstruct void someMethod(){}

    被@PostConstruct修饰的方法会在服务器加载Servle的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。PreDestroy()方法在destroy()方法执行执行之后执行

 

 

 

被注解的Servlet生命周期

    需要注意的是,注解会多多少少地影响到服务器的启动速度。服务器在启动时候会遍历Web 应用的WEB-INF/classes下的所有class文件与WEB-INF/lib下的所有jar文件,以检查哪些类使用了注解。如果应用程序中没有 使用任何注解,可以在Web.xml中设置的metadata-complete属性为true.(支持@PostConstruct和 @PreDestroy的服务器需要支持Servlet2.5规范。Tomcat5.x仅支持Servlet2.4规范。)

 

我现在要说的是用实例说明它有什么作用。

比如说我有一种情况,在我的servlet初始化加载之前我想处理一些东西,像加载缓存等等。

怎么做。@PostConstruct就派上用场了。那为什么这玩意用的不多呢,这是因为如果初始化之前我们要加载或处理某些玩意完全可以在构造器初始化时就处理了,但这种方法需要自己重写构造器。好吧。直接上代码看看具体用它的时候怎么做的。

 

复制代码
1 ackage com.whaty.products.whatysns.web.info;  
 2   
 3 import javax.annotation.PostConstruct;  
 4 import javax.annotation.Resource;  
 5   
 6 import org.springframework.stereotype.Service;  
 7 import org.springframework.util.Assert;  
 8   
 9 import com.whaty.framework.cache.core.model.Cache;  
10 import com.whaty.framework.cache.core.service.CacheService;  
11 import com.whaty.framework.cache.entitycache.service.EntityCacheHelper;  
12 import com.whaty.framework.cache.entitycache.service.IEntityDaoAdapter;  
13   
14 /** 
15  * @author bc_qi 
16  * @param <KEY> 
17  * @param <ENTITY> 
18  */  
19 @Service("AjaxCacheableService")  
20 public class AjaxCacheableService{  
21       
22     @Resource(name="cacheService")  
23     protected CacheService cacheService;  
24       
25     protected boolean useReadWriteEntityDao = false;  
26     protected boolean useCache = true;  
27     protected int entityCacheMaxSize = 1000;  
28     protected int entityCacheMaxLiveSeconds = 3600;  
29     protected Cache entityCache;  
30       
31       
32     /** 
33      * 构造方法执行后,初始化, 
34      */  
35     @PostConstruct  
36     public void init() {  
37         Assert.notNull(cacheService, "cacheService must be set!");  
38         getCache();  
39     }  
40   
41     /** 
42      * 获取cache 
43      * @return 
44      */  
45     protected Cache getCache() {  
46         if (entityCache == null) {  
47             entityCache = cacheService.addCache(this.getClass().getName(),entityCacheMaxLiveSeconds);  
48         }  
49         return entityCache;  
50     }  
51       
52     /** 
53      * @param id 
54      * @param useCache 是否使用Cache 
55      * @return 
56      */  
57     public Object getCache(String id) {  
58         String strid = String.valueOf(id);  
59         Object o = entityCache.get(strid);  
60         return  o;  
61     }  
62       
63     public Object putCache(int tTLSeconds,String cacheId,Object value) {  
64         String strid = String.valueOf(cacheId);  
65         Object o = entityCache.get(strid);  
66         if (o != null) {  
67             return  o;  
68         } else {  
69             entityCache.put(strid, value, tTLSeconds);  
70             return value;  
71         }  
72     }  
73       
74 }
复制代码

C ++中的Struct构造函数?

C ++中的Struct构造函数?

struct是否可以在C ++中使用构造函数?

我一直试图解决这个问题,但我没有得到语法。


#1楼

struct TestStruct {
        int id;
        TestStruct() : id(42)
        {
        }
};

#2楼

在C ++中, classstruct之间的唯一区别是成员和基类在类中默认是私有的,而默认情况下它们在结构中是公共的。

所以结构体可以有构造函数,语法与类相同。


#3楼

是。 结构就像一个类,但默认为public: :,在类定义中和继承时:

struct Foo
{
    int bar;

    Foo(void) :
    bar(0)
    {
    }
}

考虑到你的其他问题,我建议你阅读一些教程 。 他们会比我们更快,更完整地回答您的问题。


#4楼

是的,C ++中的结构和类是相同的,除了结构成员默认是公共的,而类成员默认是私有的。 你可以在一个结构中做的任何事情。

struct Foo
{
  Foo()
  {
    // Initialize Foo
  }
};

#5楼

是的,但如果你的结构是联盟,那么你就不能。 它与班级相同。

struct Example
{
   unsigned int mTest;
   Example()
   {
   }
};

工会不允许结构中的构造函数。 您可以在联合上创建构造函数。 这个问题涉及工会中的非平凡建构者。

我们今天的关于struct初始化及构造函数与class区别struct默认构造函数的分享就到这里,谢谢您的阅读,如果想了解更多关于15-struct(构造函数,重载)、@PostConstruct、@Autowired 以及构造函数的执行顺序、@postconstruct初始化的操作(转载)、C ++中的Struct构造函数?的相关信息,可以在本站进行搜索。

本文标签: